The ModuleAutoloader

Overview

Zend\Loader\ModuleAutoloader is a special implementation of the Zend\Loader\SplAutoloader interface, used by Zend\ModuleManager to autoload Module classes from different sources.

Apart from being able to autoload modules from directories, the ModuleAutoloader can also autoload modules packaged as Phar archives, which allows for packaging your modules in a single file for easier distribution. Supported archive formats are: .phar, .phar.gz, .phar.bz2, .phar.tar, .phar.tar.gz, .phar.tar.bz2, .phar.zip, .tar, tar.gz, .tar.bz2 and .zip. It is, however, recommended to avoid compressing your packages (be it either gz, bz2 or zip compression), as it introduces additional CPU overhead to every request.

Quickstart

As the ModuleAutoloader is meant to be used with the ModuleManager, for examples of it’s usage and how to configure it, please see the Module Autoloader Usage section of the ModuleManager documentation.

Configuration Options

The ModuleAutoloader defines the following options.

ModuleAutoloader Options

$options

The ModuleAutoloader expects an array of options, where each option is either a path to scan for modules, or a key/value pair of explicit module paths. In the case of explicit module paths, the key is the module’s name, and the value is the path to that module.

1
2
3
4
5
$options = array(
    '/path/to/modules',
    '/path/to/other/modules',
    'MyModule' => '/explicit/path/mymodule-v1.2'
);

Available Methods

__construct

Initialize and configure the object __construct($options = null)

Constructor Used during instantiation of the object. Optionally, pass options, which may be either an array or Traversable object; this argument will be passed to setOptions().

setOptions

Configure the module autoloader setOptions($options)

setOptions() Configures the state of the autoloader, registering paths to modules. Expects an array or Traversable object; the argument will be passed to registerPaths().

autoload

Attempt to load a Module class. autoload($class)

autoload() Attempts to load the specified Module class. Returns a boolean false on failure, or a string indicating the class loaded on success.

register

Register with spl_autoload. register()

register() Registers the autoload() method of the current instance with spl_autoload_register().

unregister

Unregister with spl_autoload. unregister()

unregister() Unregisters the autoload() method of the current instance with spl_autoload_unregister().

registerPaths

Register multiple paths with the autoloader. registerPaths($paths)

registerPaths() Register a paths to modules. Expects an array or Traversable object. For an example array, please see the Configuration options section.

registerPath

Register a single path with the autoloader. registerPath($path, $moduleName=false)

registerPath() Register a single path with the autoloader. The first parameter, $path, is expected to be a string. The second parameter, $moduleName, is expected to be a module name, which allows for registering an explicit path to that module.

getPaths

Get all paths registered with the autoloader. getPaths()

getPaths() Returns an array of all the paths registered with the current instance of the autoloader.

Examples

Please review the examples in the quick start for usage.

Table Of Contents

Previous topic

The ClassMapAutoloader

Next topic

The SplAutoloader Interface

This Page

Note: You need to stay logged into your GitHub account to contribute to the documentation.

Edit this document

Edit this document

The source code of this file is hosted on GitHub. Everyone can update and fix errors in this document with few clicks - no downloads needed.

  1. Login with your GitHub account.
  2. Go to The ModuleAutoloader on GitHub.
  3. Edit file contents using GitHub's text editor in your web browser
  4. Fill in the Commit message text box at the end of the page telling why you did the changes. Press Propose file change button next to it when done.
  5. On Send a pull request page you don't need to fill in text anymore. Just press Send pull request button.
  6. Your changes are now queued for review under project's Pull requests tab on GitHub.