In short: if the class namespace prefix is not in a list of registered namespaces, return FALSE immediately. This allows for more optimistic matching, as well as fallback to other autoloaders.
How it works?
By default ZF autoload only one namespace with prefix ‘Zend’. It allow to use any Zend_* classes in application out of the box.
But, what to do if you need autoload you own library of classes i.e. with prefix ‘My’.
There are several ways to do it:
1. You can register new namespace in application.ini:
autoloadernamespaces[] = My |
2. You can register new namespace in Bootstrap function:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
public function _initAutoload()
{
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('My_');
//or for multiple namespaces
$loader->registerNamespace(array('Foo_', 'Bar_'));
}
} |
3. You can tell Zend_Loader_Autoloader to act as a “fallback” autoloader:
$loader->setFallbackAutoloader(true); |
This method is not recommended to use.
Internally, Zend_Loader_Autoloader uses Zend_Loader::loadClass() to load classes. That method uses include() to attempt to load the given class file. include() will return a boolean FALSE if not successful — but also issues a PHP warning.
For the classes which don’t follow PEAR relationship with the filesystem, you can register you own autoload method:
$loader->pushAutoloader('my_autoloader', 'My_'); |
Append function ‘my_autoloader’ to the stack, to manage classes with the prefix ‘My_’.
Simple ‘my_autoloader’ function could look like:
function my_autoloader($class_name) {
include 'My/classes/'.$class_name . '.php';
} |