PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Using the Autoloader

The Zend_Loader class contains a method you can register with the PHP SPL autoloader. Zend_Loader::autoload() is the callback method. As a convenience, Zend_Loader provides the registerAutoload() function to register its autoload() method. If the spl_autoload extension is not present in your PHP environment, then the registerAutoload() method throws a Zend_Exception.

Example 532. Example of registering the autoloader callback method

<?php
Zend_Loader
::registerAutoload();

After registering the Zend Framework autoload callback, you can reference classes from Zend Framework without having to load them explicitly. The autoload() method uses Zend_Loader::loadClass() automatically when you reference a class.

If you have extended the Zend_Loader class, you can give an optional argument to registerAutoload(), to specify the class from which to register an autoload() method.

Example 533. Example of registering the autoload callback method from an extended class

Because of the semantics of static function references in PHP, you must implement code for both loadClass() and autoload(), and the autoload() must call self::loadClass(). If your autoload() method delegates to its parent to call self::loadClass(), then it calls the method of that name in the parent class, not the subclass.

<?php
class My_Loader extends Zend_Loader
{
    public static function 
loadClass($class$dirs null)
    {
        
parent::loadClass($class$dirs);
    }

    public static function 
autoload($class)
    {
        try {
            
self::loadClass($class);
            return 
$class;
        } catch (
Exception $e) {
            return 
false;
        }
    }
}

Zend_Loader::registerAutoload('My_Loader');

You can remove an autoload callback. The registerAutoload() has an optional second argument, which is TRUE by default. If this argument is FALSE, the autoload callback is unregistered from the SPL autoload stack.

Zend Framework