register - The StandardAutoloader

The StandardAutoloader


Zend_Loader_StandardAutoloader is designed as a » PSR-0-compliant autoloader. It assumes a 1:1 mapping of the namespace+classname to the filesystem, wherein namespace separators and underscores are translated to directory separators. A simple statement that illustrates how resolution works is as follows:

  1. $filename = str_replace(array('_', '\\'), DIRECTORY_SEPARATOR, $classname)
  2.           . '.php';

Previous incarnations of PSR-0-compliant autoloaders in Zend Framework have relied upon the include_path for file lookups. This has led to a number of issues:

  • Due to the use of include, if the file is not found, a warning is raised -- even if another autoloader is capable of resolving the class later.

  • Documenting how to setup the include_path has proven to be a difficult concept to convey.

  • If multiple Zend Framework installations exist on the include_path, the first one on the path wins -- even if that was not the one the developer intended.

To solve these problems, the StandardAutoloader by default requires that you explicitly register namespace/path pairs (or vendor prefix/path pairs), and will only load a file if it exists within the given path. Multiple pairs may be provided.

As a measure of last resort, you may also use the StandardAutoloader as a "fallback" autoloader -- one that will look for classes of any namespace or vendor prefix on the include_path. This practice is not recommended, however, due to performance implications.

Finally, as with all autoloaders in Zend Framework, the StandardAutoloader is capable of registering itself with PHP's SPL autoloader registry.

Note: Vocabulary: Namespaces vs. Vendor Prefixes
In terms of autloading, a "namespace" corresponds to PHP's own definition of namespaces in PHP versions 5.3 and above.
A "vendor prefix" refers to the practice, popularized in PHP versions prior to 5.3, of providing a pseudo-namespace in the form of underscore-separated words in class names. As an example, the class Phly_Couch_Document uses a vendor prefix of "Phly", and a component prefix of "Phly_Couch" -- but it is a class sitting in the global namespace within PHP 5.3.
The StandardAutoloader is capable of loading either namespaced or vendor prefixed class names, but treats them separately when attempting to match them to an appropriate path.

Quick Start

Basic use of the StandardAutoloader requires simply registering namespace/path pairs. This can either be done at instantiation, or via explicit method calls after the object has been initialized. Calling register() will register the autoloader with the SPL autoloader registry.

By default, the class will register the "Zend" namespace to the directory above where its own classfile is located on the filesystem.

Example #1 Manual Configuration

  1. // This example assumes ZF is on your include_path.
  2. // You could also load the autoloader class from a path relative to the
  3. // current script, or via an absolute path.
  4. require_once 'Zend/Loader/StandardAutoloader.php';
  5. $loader = new Zend_Loader_StandardAutoloader();
  7. // Register the "Phly" namespace:
  8. $loader->registerNamespace('Phly', APPLICATION_PATH . '/../library/Phly');
  10. // Register the "Scapi" vendor prefix:
  11. $loader->registerPrefix('Scapi', APPLICATION_PATH . '/../library/Scapi');
  13. // Optionally, specify the autoloader as a "fallback" autoloader;
  14. // this is not recommended.
  15. $loader->setFallbackAutoloader(true);
  17. // Register with spl_autoload:
  18. $loader->register();

Example #2 Configuration at Instantiation

The StandardAutoloader may also be configured at instantiation. Please note:

  • The argument passed may be either an array or a Traversable object (such as a Zend_Config object.

  • The argument passed is also a valid argument for passing to the setOptions() method.

The following is equivalent to the previous example.

  1. require_once 'Zend/Loader/StandardAutoloader.php';
  2. $loader = new Zend_Loader_StandardAutoloader(array(
  3.     'namespaces' => array(
  4.         'Phly' => APPLICATION_PATH . '/../library/Phly',
  5.     ),
  6.     'prefixes' => array(
  7.         'Scapi' => APPLICATION_PATH . '/../library/Scapi',
  8.     ),
  9.     'fallback_autoloader' => true,
  10. ));
  12. // Register with spl_autoload:
  13. $loader->register();

Configuration Options

The StandardAutoloader defines the following options.

StandardAutoloader Options


An associative array of namespace/path pairs. The path should be an absolute path or path relative to the calling script, and contain only classes that live in that namespace (or its subnamespaces). By default, the "Zend" namespace is registered, pointing to the arent directory of the file defining the StandardAutoloader.


An associative array of vendor prefix/path pairs. The path should be an absolute path or path relative to the calling script, and contain only classes that begin with the provided vendor prefix.


A boolean value indicating whether or not this instance should act as a "fallback" autoloader (i.e., look for classes of any namespace or vendor prefix on the include_path). By default, false.

Available Methods


Please review the examples in the quick start for usage.


© 2006-2018 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.