Last updated 2 June 2014. Created on 22 April 2013.
Edited by donquixote. Log in to edit this page.

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

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

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

Looking for support? Visit the forums, or join #drupal-support in IRC.