Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP __autoload Cannot redeclare [duplicate]

Tags:

php

autoload

I started to use the PHP __autoload function and now I'm getting those weird Fatal error: Cannot redeclare class xxx errors.

It's weird since these errors occur on classes I don't even load with the autoload function. And I also use require_once to include the files.

I'm really puzzled about this. Anyone knows anything about this kind of errors when using autoload?

like image 233
KingIsulgard Avatar asked Mar 03 '26 03:03

KingIsulgard


1 Answers

require_once/include_once only looks at the file name when they're trying to include a file, not a class name. So you can have class Foo in both Foo.php and B.php, and then you'll get that error.

I'm not sure how __autoload would cause you any problems, unless __autoload requires Foo.php because it needs class Foo, and you require B.php manually which redefines class Foo.

By the way, use spl_autoload_register instead of __autoload.

like image 56
Ionuț G. Stan Avatar answered Mar 04 '26 18:03

Ionuț G. Stan