Basic Autoloader Usage

Resource Autoloading

Often, when developing an application, it's either difficult to package classes in the 1:1 classname:filename standard Zend Framework recommends, or it's advantageous for purposes of packaging not to do so. However, this means you class files will not be found by the autoloader.

If you read through the design goals for the autoloader, the last point in that section indicated that the solution should cover this situation. Zend Framework does so with Zend_Loader_Autoloader_Resource.

A resource is just a name that corresponds to a component namespace (which is appended to the autoloader's namespace) and a path (which is relative to the autoloader's base path). In action, you'd do something like this:

  1. $loader = new Zend_Application_Module_Autoloader(array(
  2.     'namespace' => 'Blog',
  3.     'basePath'  => APPLICATION_PATH . '/modules/blog',
  4. ));

Once you have the loader in place, you then need to inform it of the various resource types it's aware of. These resource types are simply pairs of subtree and prefix.

As an example, consider the following tree:

  1. path/to/some/resources/
  2. |-- forms/
  3. |   `-- Guestbook.php        // Foo_Form_Guestbook
  4. |-- models/
  5. |   |-- DbTable/
  6. |   |   `-- Guestbook.php    // Foo_Model_DbTable_Guestbook
  7. |   |-- Guestbook.php        // Foo_Model_Guestbook
  8. |   `-- GuestbookMapper.php  // Foo_Model_GuestbookMapper

Our first step is creating the resource loader:

  1. $loader = new Zend_Loader_Autoloader_Resource(array(
  2.     'basePath'  => 'path/to/some/resources/',
  3.     'namespace' => 'Foo',
  4. ));

Next, we need to define some resource types. Zend_Loader_Autoloader_Resourse::addResourceType() has three arguments: the "type" of resource (an arbitrary string), the path under the base path in which the resource type may be found, and the component prefix to use for the resource type. In the above tree, we have three resource types: form (in the subdirectory "forms", with a component prefix of "Form"), model (in the subdirectory "models", with a component prefix of "Model"), and dbtable (in the subdirectory "models/DbTable", with a component prefix of "Model_DbTable"). We'd define them as follows:

  1. $loader->addResourceType('form', 'forms', 'Form')
  2.        ->addResourceType('model', 'models', 'Model')
  3.        ->addResourceType('dbtable', 'models/DbTable', 'Model_DbTable');

Once defined, we can simply use these classes:

  1. $form      = new Foo_Form_Guestbook();
  2. $guestbook = new Foo_Model_Guestbook();

Note: Module Resource Autoloading
Zend Framework's MVC layer encourages the use of "modules", which are self-contained applications within your site. Modules typically have a number of resource types by default, and Zend Framework even recommends a standard directory layout for modules. Resource autoloaders are therefore quite useful in this paradigm -- so useful that they are enabled by default when you create a bootstrap class for your module that extends Zend_Application_Module_Bootstrap. For more information, read the Zend_Loader_Autoloader_Module documentation.


Basic Autoloader Usage