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

Zend Framework 101: Zend_Loader

Creating Your Own Auto-Loader

If you have a different scheme for how your files are stored (for instance, you might use ClassName.class.php instead of ClassName.php), you can create your own auto-loader. To do so, you must create a class with a method called autoload() that accepts the name of a class as its only argument.

Listing 3 shows an example of such a class. Let's assume you store it in a file called MyAutoloader.php in your include directory.

Listing 3 Defining your own auto-loader (MyAutoloader.php)
<?php
    class MyAutoloader
    {
        public static function autoload($class)
        {
            // define your logic in here for loading the class file
        }
    }
?>

Next you must enable the auto-loader, as shown in Listing 4. Note that still only include Zend_Loader, since the registerAutoload() method will take care of loading your auto-loader.

Listing 4 Register your custom auto-loader (listing-4.php)
<?php
    require_once('Zend/Loader.php');
 
    Zend_Loader::registerAutoload('MyAutoLoader');
?>

In This Article