Chapter 17. Zend_Loader

Table of Contents

17.1. Loading Files and Classes Dynamically
17.1.1. Loading Files
17.1.2. Loading Classes
17.1.3. Testing if a File is Readable
17.1.4. Using the Autoloader

17.1. Loading Files and Classes Dynamically

The Zend_Loader class includes methods to help you load files dynamically.

[Tip] Zend_Loader vs. require_once()

The Zend_Loader methods are best used if the filename you need to load is variable. For example, if it is based on a parameter from user input or method argument. If you are loading a file or a class whose name is constant, there is no benefit to using Zend_Loader over using traditional PHP functions such as require_once().

17.1.1. Loading Files

The static method Zend_Loader::loadFile() loads a PHP file. The file loaded may contain any PHP code. The method is a wrapper for the PHP function include(). This method throws Zend_Exception on failure, for example if the specified file does not exist.

Example 17.1. Example of loadFile() method

<?php

Zend_Loader::loadFile($filename, $dirs=null, $once=false)

?>

The $filename argument specifies the filename to load, which must not contain any path information. A security check is performed on $filename. The $filename may only contain alphanumeric characters, dashes ("-"), underscores ("_"), or periods ("."). No such restriction is placed on the $dirs argument.

The $dirs argument specifies directories to search for the file. If NULL, only the include_path is searched. If a string or an array, the directory or directories specified will be searched, and then the include_path.

The $once argument is a boolean. If TRUE, Zend_Loader::loadFile() uses the PHP function include_once() for loading the file, otherwise the PHP function include() is used.

17.1.2. Loading Classes

The static method Zend_Loader::loadClass($class, $dirs) loads a PHP file and then checks for the existance of the class.

Example 17.2. Example of loadClass() method

<?php

Zend_Loader::loadClass('Container_Tree',
    array(
        '/home/production/mylib',
        '/home/production/myapp'
    )
);

?>

The string specifying the class is converted to a relative path by substituting directory separates for underscores, and appending '.php'. In the example above, 'Container_Tree' becomes 'Container/Tree.php'.

If $dirs is a string or an array, Zend_Loader::loadClass() searches the directories in the order supplied. The first matching file is loaded. If the file does not exist in the specified $dirs, then the include_path for the PHP environment is searched.

If the file is not found or the class does not exist after the load, Zend_Loader::loadClass() throws a Zend_Exception.

Zend_Loader::loadFile() is used for loading, so the class name may only contain alphanumeric characters and the hyphen ('-'), underscore ('_'), and period ('.').

17.1.3. Testing if a File is Readable

The static method Zend_Loader::isReadable($pathname) returns TRUE if a file at the specified pathname exists and is readable, FALSE otherwise.

Example 17.3. Example of isReadable() method

<?php

if (Zend_Loader::isReadable($filename)) {
    // do something with $filename
}

?>

The $filename argument specifies the filename to check. This may contain path information. This method is a wrapper for the PHP function is_readable(). The PHP function does not search the include_path, while Zend_Loader::isReadable() does.

17.1.4. Using the Autoloader

The Zend_Loader class contains a method you can register with the PHP SPL autoloader. Zend_Loader::autoload() is the method.

Example 17.4. Example of registering the autoloader callback method

<?php

spl_autoload_register(array('Zend_Loader', 'autoload'));

?>

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