PHP Autoloader

When working on a large project with a number of backend tasks, intranet and public websites, I found that a couple of third party libraries had conflicting names, so when the autoloader ran, it always found the one it hit first. I found myself wondering if it would be possible to Autoload by scope, by registering each registry to the file that had the declaration. That way if a directory was declared in a given file, it will always prefer that, and thus load the right instance. It, unfortunately, does *not* solve your problem if you need to use both instances inside of a single execution.

The second feature is due to the fact the autoloader executes outside the normal program scope, you wind up not being able to catch an error classloading… it just goes fatal. So to work around that, I build a dummy object that throws an exception on construction, so that it happens inside of a scope it can be caught in.

_import('path.to.package.*');

or

Autoloader::register_directory('./path/to/package/');

Download the autoloader.

I tried to make it as simple and versatile as possible, and I’m looking into automatic namespaces. Ask if you have any questions.

Leave a Reply