Disable OPCache temporarily

opcache.enable is PHP_INI_ALL which means that ini_set() does work, but only for current request to disable OPcache caching for the remainder of scripts compiled in your current request. (You can't force enabling). It reverts back to the system default for other requests. By this stage, the request script will already have been cached, unless you do the ini_set in an auto_prepend_file script.

The system defaults (PHP_INI_SYSTEM) are latched as part of PHP system startup and can't be reread. So in the case of Apache for example, you need to restart Apache to change / reload these.

The .htaccess php_flag directives only apply if you are running mod_php or equivalent. They and .user.ini files are PHP_INI_PERDIR, which will also be latched at request activation.

Now to the Q that I think that you might be asking. If you have a dev system then the easiest way is to set opcache.enable=0 in the appropriate INI file and restart your webserver. Set it back to =1 and restart again when you are done.

Also consider (in the dev context) setting opcache.validate_timestamps=on and opcache.revalidate_freq=0. This will keep OPcache enabled but scripts will be stat'ed on every compile request to see if they are changed. This gives the best of both worlds when developing.

Also read up on the opcache.blacklist_filename directive. This allow you to specify an exclusion file, so if this contains /var/www/test, and the web service docroot is /var/www then any scripts in the /var/www/test* hierarchies will not be cached.


The best way i found in my case for disable opcache in a specific PHP file is : opcache_invalidate(__FILE__, true);

You also can reset all cache with PHP : opcache_reset();


Once your script runs, it's too late to not cache the file. You need to set it outside PHP:

  • If PHP runs as Apache module, use an .htaccess file:

    php_flag opcache.enable Off
    
  • If PHP runs as CGI/FastCGI, use a .user.ini file:

    opcache.enable=0
    

In all cases, you can also use good old system-wide php.ini if you have access to it.

Tags:

Php

Opcache