Automatically loading class with Zend_Loader

Introduction

In this article I will introduce you to Zend_Loader, a Zend Framework class used to automatically load other classes. Typically you would use include_once or require_once when you want to load a class but using Zend_Loader you don’t have to.

For class auto-loading to work (see http://php.net/autoload for full details), your classes must be named and filed in an orderly fashion. The Zend_Loader class requires all classes to be named in the same fashion as classes in the Zend Framework.

Zend_Loader treats a class name as a file system path, replacing any underscores with a slash and appending .php to the end. For example, the class name Zend_Loader would map to the file system path Zend/Loader.php.

Note: This is relative to your PHP include path, which can be defined with the include_path configuration directive.

This article requires PHP 5.2 or newer and Zend Framework. You can download the Zend Framework from http://framework.zend.com. At time of writing, the current version of Zend Framework is 1.7.6.

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!

<?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.

<?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();

?>

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.

<?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.

<?php

    require_once(‘Zend/Loader.php’);

    Zend_Loader::registerAutoload(‘MyAutoLoader’);

?>

Checking If A Class Is Defined

If you want to check whether or not a class has already been defined, you can use the class_exists() method. An important thing to note about class_exists() is that if the class does not exist then PHP will automatically invoke the auto-loader. To disable this behaviour, you must pass false as the second argument to class_exists().

Listing 5 demonstrates this behaviour.

<?php

    require_once(‘Zend/Loader.php’);

    Zend_Loader::registerAutoload();

    if (!class_exists(‘MyTestClass’)) {

       // class doesn’t exist, but now the auto-loader will try and load it

    }

    if (!class_exists(‘MyOtherTestClass’, false)) {

        // class doesn’t exist, and the auto-loader will not be used

    }

?>

Summary

In this article I showed you how to simplify your application development by making using of the Zend_Loader class that comes with the Zend Framework. I showed you how to enable and disable the loader, as well as how to define your own custom loaders.

Further reading:

Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedIn

Leave a Reply

Your email address will not be published. Required fields are marked *