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

Using the Autoloader

The first time an instance of the autoloader is retrieved, it registers itself with spl_autoload. You retrieve an instance using the getInstance() method:

<?php
$autoloader 
Zend_Loader_Autoloader::getInstance();

By default, the autoloader is configured to match the "Zend_" and "ZendX_" namespaces. If you have your own library code that uses your own namespace, you may register it with the autoloader using the registerNamespace() method. For instance, if your library code is prefixed with "My_", you could do so as follows:

<?php
$autoloader
->registerNamespace('My_');

Namespace Prefixes

You'll note that the previous example uses "My_" and not "My". This is because Zend_Loader_Autoloader is intended as a general purpose autoloader, and does not make the assumption that a given class prefix namespace includes an underscore. If your class namespace does include one, you should include it when registering your namespace.

You can also register arbitrary autoloader callbacks, optionally with a specific namespace (or group of namespaces). Zend_Loader_Autoloader will attempt to match these first before using its internal autoloading mechanism.

As an example, you may want to utilize one or more eZcomponents components with your Zend Framework application. To use its autoloading capabilities, push it onto the autoloader stack using pushAutoloader():

<?php
$autoloader
->pushAutoloader(array('ezcBase''autoload'), 'ezc');

This tells the autoloader to use the eZcomponents autoloader for classes beginning with "ezc".

You can use the unshiftAutoloader() method to add the autoloader to the beginning of the autoloader chain.

By default, Zend_Loader_Autoloader does no error suppression when using its internal autoloader, which utilizes Zend_Loader::loadClass(). Most of the time, this is exactly what you want. However, there may be cases where you want to suppress them. You can do this using suppressNotFoundWarnings():

<?php
$autoloader
->suppressNotFoundWarnings(true);

Finally, there may be times when you want the autoloader to load any namespace. For instance, PEAR libraries do not share a common namespace, making specifying individual namespaces difficult when many PEAR components are in use. You can use the setFallbackAutoloader() method to have the autoloader act as a catch-all:

<?php
$autoloader
->setFallbackAutoloader(true);

Loading Classes from PHP Namespaces

Starting in version 1.10.0, Zend Framework now allows loading classes from PHP namespaces. This support follows the same guidelines and implementation as that found in the PHP Framework Interop Group PSR-0 reference implementation.

Under this guideline, the following rules apply:

  • Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.

  • Each "_" character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The "_" character has no special meaning in the namespace.

  • The fully-qualified namespace and class is suffixed with ".php" when loading from the file system.

As examples:

  • \Doctrine\Common\IsolatedClassLoader => /path/to/project/lib/vendor/Doctrine/Common/IsolatedClassLoader.php

  • \namespace\package\Class_Name => /path/to/project/lib/vendor/namespace/package/Class/Name.php

  • \namespace\package_name\Class_Name => /path/to/project/lib/vendor/namespace/package_name/Class/Name.php

Zend Framework