getRegisteredAutoloaders - The AutoloaderFactory

The AutoloaderFactory


Starting with version 1.12.0, Zend Framework now offers multiple autoloader strategies. Often, it will be useful to employ multiple autoloading strategies; as an example, you may have a class map for your most used classes, but want to use a PSR-0 style autoloader for 3rd party libraries.

While you could potentially manually configure these, it may be more useful to define the autoloader configuration somewhere and cache it. For these cases, the AutoloaderFactory will be useful.

Quick Start

Configuration may be stored as a PHP array, or in some form of configuration file. As an example, consider the following PHP array:

  1. $config = array(
  2.     'Zend_Loader_ClassMapAutoloader' => array(
  3.         'application' => APPLICATION_PATH . '/autoload_classmap.php',
  4.         'zf'          => APPLICATION_PATH . '/../library/Zend/autoload_classmap.php',
  5.     ),
  6.     'Zend_Loader_StandardAutoloader' => array(
  7.         'namespaces' => array(
  8.             'Phly\Mustache' => APPLICATION_PATH . '/../library/Phly/Mustache',
  9.             'Doctrine'      => APPLICATION_PATH . '/../library/Doctrine',
  10.         ),
  11.     ),
  12. );

An equivalent INI-style configuration might look like the following:

  1. Zend_Loader_ClassMapAutoloader.application = APPLICATION_PATH "/autoload_classmap.php"
  2. Zend_Loader_ClassMapAutoloader.zf          = APPLICATION_PATH "/../library/Zend/autoload_classmap.php"
  3. Zend_Loader_StandardAutoloader.namespaces.Phly\Mustache = APPLICATION_PATH "/../library/Phly/Mustache"
  4. Zend_Loader_StandardAutoloader.namespaces.Doctrine       = APPLICATION_PATH "/../library/Doctrine"

Once you have your configuration in a PHP array, you simply pass it to the AutoloaderFactory.

  1. // This example assumes ZF is on your include_path.
  2. // You could also load the factory class from a path relative to the
  3. // current script, or via an absolute path.
  4. require_once 'Zend_Loader_AutoloaderFactory.php';
  5. Zend_Loader_AutoloaderFactory::factory($config);

The AutoloaderFactory will instantiate each autoloader with the given options, and also call its register() method to register it with the SPL autoloader.

Configuration Options

AutoloaderFactory Options


The AutoloaderFactory expects an associative array or Traversable object. Keys should be valid autoloader class names, and the values should be the options that should be passed to the class constructor.

Internally, the AutoloaderFactory checks to see if the autoloader class referenced exists. If not, it will use the StandardAutoloader to attempt to load the class via the include_path (or, in the case of "Zend"-namespaced classes, using the Zend Framework library path). If the class is not found, or does not implement the SplAutoloader interface, an exception will be raised.

Available Methods


Please see the Quick Start for a detailed example.


© 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.