X Autoload

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

xautoload is a PHP class loading suite.
Most importantly, it provides Drupal-8-style PSR-4 autoloading for Drupal 7.

Patterns supported by default

If you follow one of the patterns below, your classes will load automatically.

D8-style PSR-4
Class: 'Drupal\' . $module . '\SubNamespace\SomeClass'
File: $module_dir . '/src/SubNamespace/SomeClass.php'

See also https://drupal.org/node/2156625

Old D8-style PSR-0
This is no longer supported in Drupal 8 since 8.0-alpha12, but xautoload will continue to support it for D7 for backwards compatibility with existing D7 modules.

Class: 'Drupal\' . $module . '\SubNamespace\SomeClass'
File: $module_dir . '/lib/Drupal/' . $module . '/SubNamespace/SomeClass.php'

See also http://drupal.org/node/1320394

Custom "PEAR-FLAT" pattern for PHP 5.2 compatibility
Since namespaces are/were not supported in PHP before 5.3, xautoload supports a solution that is entirely based on underscores. It is similar to the PEAR pattern, but with flat directories:

Class: $module . '_Some_Class'
File: $module_dir . '/lib/Some/Class.php'

Even if PHP 5.2 could be considered dead by now, there are still enough modules out there that use this PEAR-FLAT standard, in an attempt to be compatible with PHP 5.2.

Basic usage

Download / install / enable.
drush dl xautoload
drush en xautoload

Optionally, enable APC cache:
drush vset autoloader_mode apc
or visit Admin > Configuration > Development > Performance
(The cache will only have an effect if you have APC installed.)

Add as a dependency in your modules that need it.
dependencies[] = xautoload

Alternative files[] wildcard syntax for module info files

xautoload, in addition to the other class loading stuff, enables a wildcard syntax for the files[] directive, in case you don't want the

Early-bootstrap class loading (expert only).

X Autoload has some tricks to enable class loading even before it would be regularly available.

Registration of additional (3rd party) namespaces.

There are multiple ways in

XAutoload compared to Drupal 7 autoloading

Class loading with the Drupal 7 "registry"

XAutoload performance tuning

XAutoload has a range of performance options, that can be configured on admin/config/development/performance.

Guide maintainers

donquixote's picture