The autoload module - is utility. It allows other modules to leverage PHP5 class autoloading capabilities in a unified fashion.

Requirements:

  • PHP 5.1.2 for Drupal 6 (unsupported)
  • PHP 5.3.0 for Drupal 7

You do not need this module unless you are developing a module that makes use of classes or you are installing a module that depends on this one.

PLEASE, READ THE DOCUMENTATION BEFORE USAGE!

You able to use one or both of known autoloading standards - PSR-0 and PSR-4.

CTools API is already used this functionality.

Drupal way

Open *.info file and add autolaod = TRUE there. Now just place the objects into lib/Drupal/YOUR_MODULE_NAME or src.

; All namespaces must be followed by "Drupal\YOUR_MODULE_NAME".
autoload = TRUE

Have a look at tests as an example.

Custom namespaces

Open *.info file and configure the autoload directive, where keys are subdirectories inside of the module directory and values - are namespaces. As much as needed directories and namespace bases could be added by such way.

PSR-0

; All objects, namespace path of which starts from "CTools",
; will be searched inside of "<MODULE_PATH>/psr-0".
autoload[psr-0][] = CTools

PSR-4

; All objects, namespace path of which starts from "CTools\Plugins",
; will be searched inside of "<MODULE_PATH>/psr-4".
autoload[psr-4][] = CTools\Plugins

PSR-4 (single namespace)

Take into account the trailing slash! It must be at the end of global namespace to use PSR-4 standard. It looks similar to PSR-0, but that slash telling that it's not true.

; All objects, namespace path of which starts from "Example",
; will be searched inside of "<MODULE_PATH>/psr-4-single".
autoload[psr-4-single][] = Example\

Have a look at tests as an example.

Supporting organizations: 

Project Information

Downloads