开发者

PHP with APC: Fatal errors: Cannot redeclare class

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


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 :)!


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).


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.


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.


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.


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


I just had this happen to me, and I didn't find the solution suggested in any of the other answers. I am using various autoloaders including Composer autoloader and an older version of the Zend Framework Autoloader.

The problem turned out to be caused by a slight name mismatch between the file name and the class name. One character different between the class name and the file name - a discrepancy that a human could easily miss but the a couple of autoloaders successively included the same file, causing the error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜