Setting up an __autoload()er

Now that we’ve setup an include path (see Directory and Include Path Setup), we’re going to want to set up an __autoload() function to help us automate loading class files.

Consider the following code to load a class file and instantiate it:

<?php
    require( "class.user.php" );
 
    $oUser = new User();
?>

In order to instantiate a class, we first have to load the file that defines that class. This can be an annoying process if we have a code path that could potentially use dozens of classes, but may only use a few. We would have to require all of the class files regardless of whether or not the code path uses any of them.

But when we set up an __autoload() function, we let PHP load the class files for us on demand, which means that only those files needed are loaded.

Our __autoload()er

The syntax of the __autoload() function looks like the following:

<?php
    function __autoload( $sClassName )
    {
        // your code here!
    }
?>

When we attempt to use a class that has not yet been defined (loaded), PHP will check for the presense of an __autoload() function. If found, PHP will pass the name of the class to this function and assumes that the function will load the class file for it (or error out).

Our autloader will use our include path and loop each directory in it to check for the class file. Although we do not store classes in every directory (such as images/ or views/), for simplicity, we will use it anyway. If you are concerned about performance, you can write custom code in your __autoload() method to only search those directories that would logically have class files in them.

Our generic autoloader looks like this:

<?php
    function __autoload( $sClassName )
    {
        $aIncludePath = explode( PATH_SEPARATOR, get_include_path() );
 
        $sClassFile = "class." . strtolower( $sClassName ) . ".php";
 
        foreach( $aIncludePath as $sPath )
        {
            if( file_exists( "{$sPath}/{$sClassFile}" ) )
            {
                require( "{$sPath}/{$sClassFile}" );
                return;
            }
        }
 
        throw new FileNotFoundException( "Class {$sClassName} not found" );
    }
?>

The autoloader first creates an array from the include path, using PHP’s get_include_path() function, exploding on the PHP define PATH_SEPARATOR.

All OpenAvanti class file names are formatted as: class.[class_name].php. We will assume you name your classes the same.

We loop each directory in the include path, and determine whether or not the file $sPath/$sClassFile exists. If it does, we require the file and stop. If not, we continue searching.

If at the end of the loop, we still have not found a class file to require, we throw a FileNotFoundException (which is part of the OpenAvanti framework) as we can go no further without a class definition. If we’re doing everything right, we should never get this exception.

Now, instead of manually including the class file, we can just do this:

<?php	
    $oUser = new User();
?>

If we have not already loaded the class.user.php file, PHP will pass the string “User” to the autoloader, and if all is well, our autoloader will require class.user.php.

Now that we have OpenAvanti installed and setup, we’re ready to start writing an application.