I'm getting a PHP Fatal error: Cannot redeclare class Foo in /directory/ on line 20
error, but I have no idea where it's coming from. I'm always using require_once
for this class file, and I'm not sure how to debug it. Can I get some kind of inclusion stack trace somehow? I'm running PHP 5, so case sensitivity such as descriped here should not be a problem: http://www.php.net/manual/en/function.include-once.php.
Asked
Active
Viewed 2,059 times
1

Lazlo
- 8,518
- 14
- 77
- 116
-
Check http://stackoverflow.com/questions/1159216/how-can-i-get-php-to-produce-a-backtrace-upon-errors – usoban Jun 06 '11 at 14:32
-
Unfortunately I can't install PHP extensions. Moreover, debug_backtrace only tells me there's an error on require_once... – Lazlo Jun 06 '11 at 14:37
-
check also: http://www.php.net/manual/en/class.errorexception.php – usoban Jun 06 '11 at 14:38
2 Answers
3
Use debug_backtrace in file where is class declared, but before it's declaration

kravemir
- 10,636
- 17
- 64
- 111
2
Another approach is to rename the class that was redefined (also rename the file containing the class) and then fix all class-not-found errors you get from that. That should lead you to the code that is causing the redefinition. In my case it was a class_alias statement that was causing the problem.

Leven
- 530
- 3
- 10
-
1That would have helped me. In my scenario the "cannot redeclare" error was a complete red herring -- the issue was that the autoloader couldn't find some other, unrelated class. – mpen Jun 12 '15 at 17:12