PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Resource autoloader usage

Let's consider the following directory structure:

path/to/some/directory/
    acls/
        Site.php
    forms/
        Login.php
    models/
        User.php

Within this directory, all code is prefixed with the namespace "My_". Within the "acls" subdirectory, the component prefix "Acl_" is added, giving a final class name of "My_Acl_Site". Similarly, the "forms" subdirectory maps to "Form_", giving "My_Form_Login". The "models" subdirectory maps to "Model_", giving "My_Model_User".

You can use a resource autoloader to autoload these classes. To instantiate the resource autoloader, you are required to pass at the minimum the base path and namespace for the resources it will be responsible for:

<?php
$resourceLoader 
= new Zend_Loader_Autoloader_Resource(array(
    
'basePath'  => 'path/to/some/directory',
    
'namespace' => 'My',
));

Base namespace

In Zend_Loader_Autoloader, you are expected to provide the trailing underscore ("_") in your namespace if your autoloader will use it to match the namespace. Zend_Loader_Autoloader_Resource makes the assumption that all code you are autoloading will use an underscore separator between namespaces, components, and classes. As a result, you do not need to use the trailing underscore when registering a resource autoloader.

Now that we have setup the base resource autoloader, we can add some components to it to autoload. This is done using the addResourceType() method, which accepts three arguments: a resource "type", used internally as a reference name; the subdirectory path underneath the base path in which these resources live; and the component namespace to append to the base namespace. As an example, let's add each of our resource types.

<?php
$resourceLoader
->addResourceType('acl''acls/''Acl')
               ->
addResourceType('form''forms/''Form')
               ->
addResourceType('model''models/''Model');

Alternately, you could pass these as an array to addResourceTypes(); the following is equivalent to the above:

<?php
$resourceLoader
->addResourceTypes(array(
    
'acl' => array(
        
'path'      => 'acls/',
        
'namespace' => 'Acl',
    ),
    
'form' => array(
        
'path'      => 'forms/',
        
'namespace' => 'Form',
    ),
    
'model' => array(
        
'path'      => 'models/',
        
'namespace' => 'Model',
    ),
));

Finally, you can specify all of this when instantiating the object, by simply specifying a "resourceTypes" key in the options passed and a structure like that above:

<?php
$resourceLoader 
= new Zend_Loader_Autoloader_Resource(array(
    
'basePath'      => 'path/to/some/directory',
    
'namespace'     => 'My',
    
'resourceTypes' => array(
        
'acl' => array(
            
'path'      => 'acls/',
            
'namespace' => 'Acl',
        ),
        
'form' => array(
            
'path'      => 'forms/',
            
'namespace' => 'Form',
        ),
        
'model' => array(
            
'path'      => 'models/',
            
'namespace' => 'Model',
        ),
    ),
));

Zend Framework