PHP with APC: Fatal errors: Cannot redeclare class

2019-01-31 21:53发布

问题:

Since I installed APC for PHP with PECL I get sometimes these errors: Cannot redeclare class xxx

xxx changes from time to time. I could disable APC but APC improves the performance great! Is there a known bug or could I do something else to prevent these errors? I'm using Ubuntu 8.04 LTS with PHP 5.2.4.


Edit/Update (from comments):

I use the Zend Framework Autoloader and these error never occurred before I enabled APC. A few moments ago I get for example that error: Fatal error: require(): Cannot redeclare class zend_db_adapter_abstract in /paths/app/lib/Zend/Db/Select.php on line 27

回答1:

The combination of the following configs fixed it for me:

apc.include_once_override = 0
apc.canonicalize = 0
apc.stat = 0

Without all 3, I'd constantly get the error, but with all three I seem to no longer get the error :)!



回答2:

I had the same problem with a bunch of PHP libraries as soon as I enabled APC. After a lot of hair pulling I found that setting apc.include_once_override = 0 cleared things up. Still monitoring but haven't had a the problem re-occur (before that I was able to induce the error by clearing the apc cache).



回答3:

This error happened when using Amazon's AWS SDK for PHP2 in a php script running under cron. One solution was to disable apc via -d apc.enabled=0 as shown below:

/usr/bin/php -d apc.enabled=0 /path/to/myshelljob.php

For more info.



回答4:

Hmmm, seems to be a common issue:

  • Opcode (APC/XCache), Zend, Doctrine, and Autoloaders
  • http://forums.zend.com/viewtopic.php?f=69&t=813
  • http://framework.zend.com/issues/browse/ZF-8588

What I just noticed from your specific error message is that you wrote zend_db_adapter_abstract in all-lowercase. A problem with the horrid framework naming schemes and autoloaders is that they keep files in mixed case and expect it so. If your code tried to instantiate it this way, the autoloader might not have found it. APC might be more peculiar here, since it overrides include_once internally, maybe with side-effects.

A solution would be to adapt the Zend autoloader, and manually keep a list of loaded classes and (absolute and lowercased) filenames to proofcheck in lieu of include_once.

Otherwise, try excessive xdebug-ing. Without access to your setup, all we can do is guess here.



回答5:

Well it is a known problem with apc that it mixes up include_once directivse that are called relatively from different locations.

So if you do include_once myclass.php and then in a subdirectory do include_once ../myclass.php apc could mix this up and think its different files and loads it twice.

However this is fixed in later versions.

If you can drill down your code to the class that is loaded twice you could do some checking if the class is already loaded with class_defined or some callable stuff.

You can also use the apc.filter directive to prevent certain files from beeing cached at all.



回答6:

Download the latest apc version and use:

[APC]
apc.cache_by_default = 0

With apc.stat = 0 the server loads the php files in cache, if you modify it, php still loads the same.

More info:

  • https://bugs.php.net/bug.php?id=58878#1275406932
  • http://www.php.net/manual/en/apc.configuration.php


标签: apc php