PhpRiot
Download Article
Download this article or the entire “Zend Framework 101” series with all listings and files.




More information
Related Books
Zend Framework, A Beginner's Guide

Zend Framework, A Beginner's Guide

Essential Skills--Made Easy! Leverage the power of the Zend Framework to supercharge...

Zend Framework 1.8 Web Application Development

Zend Framework 1.8 Web Application Development

Design, develop, and deploy feature-rich PHP web applications with this MVC framework...
PhpRiot Newsletter
Your Email Address:

More information

Zend Framework 101: Zend_Loader

Enabling and Disabling the Auto Loader

In order to enable the auto-loader, use the Zend_Loader::RegisterAutoload() static method. This method optionally accepts two arguments: the name of the auto-loader class, and whether to enable or disable the auto-loader.

By default the Zend_Loader class is used as the first argument, but you can use your own class if you desire. If you do, you must ensure your class defines a public static method called autoload(). This method accepts a single argument, which is the name of the class to load. An example of this is shown later in this article.

Listing 1 shows how to enable the autoloader. Note that following this you can use any class you like without needing to call require_once(). Note that you must initially include the Zend_Loader class for this to work!

Listing 1 Enabling the auto-loader (listing-1.php)
<?php
    require_once('Zend/Loader.php');
    Zend_Loader::registerAutoload();
 
    $myFooBar = new Foo_Bar();
?>

In this example, the Foo_Bar class would be defined by the file Foo/Bar.php in your include path.

To disable the auto-loader, you pass false as the second argument to registerAutoload(). Note that you must also explicitly specify the class name in order to achieve this, which wasn't required when we enabled it.

Listing 2 demonstrates disabling the auto-loader. Note that after the auto-loader is disabled you must manually include class files as required.

Listing 2 Disabling the auto-loader (listing-2.php)
<?php
    // assume the auto-loader has already been enabled prior
 
    Zend_Loader::registerAutoload('Zend_Loader', false);
 
    require_once('Foo/Other.php');
    $myFooOther = new Foo_Other();
?>

In This Article