talideon.com

I can be magnanimous!

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

Technorati Search Technorati Search Irish Bloggers

Comments

No comments.

Post a comment

All form information is optional, but it’s a good idea to fill in your name and email address if you want me to take your comment seriously.

Spammers, don’t bother posting crap down here. The site is set up so that legitimate search engines (Google, for instance) won’t index pages with comments on them. Posting crud here only means you’re wasting my time and patience. Shoo!

Real names, please. Please include!
Won’t be displayed. Please include!
Displayed, if present.