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

Basic Use Case

First, let's assume the following directory structure and class files, and that the top level directory and library directory are on the include_path:

application/
    modules/
        foo/
            views/
                helpers/
                    FormLabel.php
                    FormSubmit.php
        bar/
            views/
                helpers/
                    FormSubmit.php
library/
    Zend/
        View/
            Helper/
                FormLabel.php
                FormSubmit.php
                FormText.php

Now, let's create a plugin loader to address the various view helper repositories available:

<?php
$loader 
= new Zend_Loader_PluginLoader();
$loader->addPrefixPath('Zend_View_Helper''Zend/View/Helper/')
       ->
addPrefixPath('Foo_View_Helper',
                       
'application/modules/foo/views/helpers')
       ->
addPrefixPath('Bar_View_Helper',
                       
'application/modules/bar/views/helpers');

We can then load a given view helper using just the portion of the class name following the prefixes as defined when adding the paths:

<?php
// load 'FormText' helper:
$formTextClass $loader->load('FormText'); // 'Zend_View_Helper_FormText';

// load 'FormLabel' helper:
$formLabelClass $loader->load('FormLabel'); // 'Foo_View_Helper_FormLabel'

// load 'FormSubmit' helper:
$formSubmitClass $loader->load('FormSubmit'); // 'Bar_View_Helper_FormSubmit'

Once the class is loaded, we can now instantiate it.

Note

In some cases, you may use the same prefix for multiple paths. Zend_Loader_PluginLoader actually registers an array of paths for each given prefix; the last one registered will be the first one checked. This is particularly useful if you are utilizing incubator components.

Paths may be defined at instantiation

You may optionally provide an array of prefix / path pairs (or prefix / paths -- plural paths are allowed) as a parameter to the constructor:

<?php
$loader 
= new Zend_Loader_PluginLoader(array(
    
'Zend_View_Helper' => 'Zend/View/Helper/',
    
'Foo_View_Helper'  => 'application/modules/foo/views/helpers',
    
'Bar_View_Helper'  => 'application/modules/bar/views/helpers'
));

Zend_Loader_PluginLoader also optionally allows you to share plugins across plugin-aware objects, without needing to utilize a singleton instance. It does so via a static registry. Indicate the registry name at instantiation as the second parameter to the constructor:

<?php
// Store plugins in static registry 'foobar':
$loader = new Zend_Loader_PluginLoader(array(), 'foobar');

Other components that instantiate the PluginLoader using the same registry name will then have access to already loaded paths and plugins.

Zend Framework