talideon.com

Blackout Ireland

November 19, 2007 at 11:21AM Note to self on PHP autoloaders

It appears that if you’re in an autoloader, you can’t depend on the autoloader to work, that is, autoloaders don’t work within the context of an autoloader executing.

This is a little frustrating at times but makes sense as you could end up with accidental infinite recursion if you try to load a class--say an exception class--directly or indirectly. If the load fails, and your autoloader tries to load the very same class, it’ll fail and PHP might call the autoloader again. So PHP wisely makes them non-reentrant. Reentrancy is something you normally want, but in this case, due to the quasimagical nature of autoloaders, reentrancy would make problems with them hard to debug.

The solution? If your autoloaders need anything, make sure they’re explicitly loaded beforehand.

[For anybody who’s wondering, I ran into this when adding diagnostic exceptions to an autoloader and was depending on the autoloader to load said exceptions.]