Chapter 8. Zym_Loader

Chapter 8. Zym_Loader

Zym does not have its own loader, rather you are to use Zend_Loader since it provides all the neccessary features needed.

[Note] Note on class loading

Unobvious to many newcomers is the fact that Zend_Loader was not designed to load files as a replacement to require_once() in applications with code such as Zend_Loader::loadClass('Zend_Controller_Front');. This method provides no benefits from a plain require_once('Zend/Controller/Front.php'); in these situations. In fact, using it unnecessarily can have a significant impact on performance.

Basically, the point is to avoid using Zend_Loader in situations where a require/include would work better unless you need its features. It was designed for you to benefit from these features, not be hindered by them.

8.1. Autoloading

This component provides a solid interface and autoload implementations for commonly used libraries and directory structures through Zend_Loader, which provides an api for registering spl class autoloaders through Zend_Loader::registerAutoload();.

[Note] Note

It is recommended that you use this method over __autoload() as your code will be much clearer and cleaner because multiple autoloaders can be registered.

8.1.1. Registering an autoloader

Autoloaders are registered through Zend_Loader::registerAutoload(); or through SPL with spl_autoload_register(array('Zym_Loader_Autoload_Doctrine', 'autoload'));.

Multiple loaders are pushed onto the spl autoload stack in FIFO order, so organizing the order you push loaders onto the stack may have a large effect on performance depending on the setup.

[Note] Note

See PHP.net spl_autoload_register() for more information on SPL's autoloading.

Example 8.1. Registering autoloaders

<?php
/**
* @see Zend_Loader
*/
require_once 'Zend/Loader.php';
 
// Register Zend's autoloader
Zend_Loader::registerAutoload();
 
// Register Zym Doctrine autoloader
Zend_Loader::registerAutoload('Zym_Loader_Autoload_Doctrine');
 
// Unregister Zym Doctrine autoloader
Zend_Loader::registerAutoload('Zym_Loader_Autoload_Doctrine', false);

8.1.2. Writing your own autoloaders

Creating autoloaders is a fairly trivial task, all that is needed is that an autoloader implement Zym_Loader_Autoload_Interface; however, Zend_Loader does not require this itself.

While Zend_Loader only requires that a class have an autoload($class) function that is compatible with spl_autoload_register(), it was Zym's goal to standardize this with an interface.

Example 8.2. A custom autoloader

<?php
/**
* @see Zym_Loader_Autoload_Interface
*/
require_once 'Zym/Loader/Autoload/Interface.php';
 
class App_Loader_Autoload_Foo
{
    /**
     * spl_autoload() suitable implementation for supporting class autoloading.
     *
     * Attach to spl_autoload() using the following:
     * <code>
     * spl_autoload_register(array('Zend_Loader', 'autoload'));
     * </code>
     *
     * @param string $class
     * @return string|false Class name on success; false on failure
     */
    public function autoload($class)
    {
        // Logic here
        // Return classname on success
        return $class;
 
        // Return false when unable to load
        return false;
    }
}