Symfony2: Weird autoloading ? Interface implementations become wrong? May be a simple APC issue to prevent
Just updated your code in a pre-prod environment, hosted on the same production env server. Purged and warmed caches, then started checking this website's new pre-production version. And... BOOM! website falls down weirdly, and your Apache2 error log outputs errors that just do not exists in your code :
PHP Classes methods declarations that suddenly become incompatible with the Interface they implement,
Lines of code mentioned in error.log do not exist or are mismatched in your PHP code. No way to debug it.
This may be a simple cache key conflict situation in your APC usage.
By example in Symfony2, APC is proposed for autoloading, to improve performance. You configure it in the web/app.php file, and comments invite you to use a unique prefix in order to prevent such cache key conflicts with other applications also using APC.
But how to easily choose a different unique prefix between prod & preprod environments ? You may use by example a PATH-based short unique prefix, using crc32:
if (!extension_loaded('apc')) { $loader = new ApcClassLoader(
sprintf('%u', hash("crc32b", __DIR__)),
$loader
);
$loader->register(TRUE);
To be 32bits/64bits switch proof, you need to ensure your prefix to be an unsigned int, using sprintf & %u.
APC is one of the most recommended Opcode cache system for PHP < 5.5. From 5.5 and after, Zend OPCache is included in PHP, so APC is no then more required, but it remains very helpful (say: mandatory) for any other previous 5.4.* versions of PHP.
This is my default personal configuration of it :
extension=apc.so apc.shm_size=128M apc.shm_segments=1 apc.max_file_size=5M apc.num_files_hint=10000 apc.slam_defense = 0 apc.write_lock=1
And this is how you may clear it manually for php-cli:
php -r "apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode');"
Note that apc_clear_cache() should be called from within the process (or child process), say a php file served by Apache2.