The Zend Framework is one of the best things to happen to PHP since the introduction of objects. But one area that’s always bugged me has been Zend_Loader, specifically its autoloader. For a framework with an extremely rigid class naming structure, you’d think the autoloader would be light and intuitive. Yet it’s a lot more complex than it needs to be for most developers, leading to some inefficient code and a lot of ‘require_once’ statements everywhere, most of which, if we play our cards right, can be deleted. So, here’s a little autoloader that I wrote for a current project that does the job in nearly every case. Assuming you’re going to make a method of your Bootstrap class, it’ll look something like:
public static function autoload($path)
{
include str_replace('_', '/', $path) . '.php';
return $path;
}
Then when you’re running the bootstrap, change your default registerAutoload() call to:
require_once 'Zend/Loader.php';
Zend_Loader::registerAutoload('Bootstrap');
And that’s it! Erase or comment out those annoying and inefficient require_once statementse. I know, slightly ironic that we need a require_once since the goal is to get rid of them. I never said it was perfect – almost.