xautoload is a comprehensive and yet highly efficient PHP class loading suite.

Most importantly, it provides Drupal-8-style PSR-0 and PSR-4 autoloading for Drupal 7. But it is also a great tool for 3rd party autoloading.

Class loading for Drupal modules:

Class loading for 3rd party libraries:

  • Libraries API: Namespace registration for libraries directly from hook_libraries_info().
    #1781794: Registration of additional PSR-0 folders in modules and sites/all/libraries
  • Can register additional PSR-4 or PSR-0 namespace directories.
  • Can scan downloaded libraries to build a classmap. (which will be cached)
  • Can read and understand composer.json files from downloaded libraries.
  • Can process composer-generated autoload files in a vendor/composer directory, to register all this stuff to the xautoload class loader.


  • Early-bootstrap mode: The class loader can be available as early as you wish, if you include it in your settings.php (or even your index.php). But even without that, it is registered damn early in the request..
  • Does not break even if you move the module to a different folder.


  • (Optional) APC cache for classes (very much like Symfony / D8).
    (using APC as a key-value store)
    (also supports WinCache and XCache)
  • Scales up to a large number of namespaces.
  • Module namespaces registration is skipped if all classes are in APC cache, saving yet another few milliseconds.
  • Benchmarks: #1912694-1: Run benchmarks


Why would you need a class loader that is that flexible and powerful? Is it not enough to support basic PSR-0, and let strange libraries ship their own class loaders?

Yes, but..
- the smaller the autoload stack, the better.
- those strange libraries can now all benefit from the APC cache.
-> profit.

Modules that use xautoload

Some of these modules have a compatibility fallback, to avoid having xautoload as an explicit requirement. Still, they should work better with xautoload enabled.

Full PSR-0:

PHP 5.2 compatibility style:

How to use

  1. Download / install / enable.
  2. Optionally, enable APC cache on admin/config/development/performance.
  3. Add as a dependency in your modules that need it.
    dependencies[] = xautoload (>= 7.x-4.0)
    Now create your PSR-0 class files, e.g.
    lib/Drupal/modulename/Foo/Bar.php => class Drupal\modulename\Foo
  4. Optionally, enable PSR-4 support for your modulename.module:
    (it is your choice if the files should be in 'lib', 'src' or something else, like 'psr4'.
    ()->registerModulePsr4(__FILE__, 'src');

    Now create your PSR-4 class files, e.g.
    src/Foo/Bar.php => class Drupal\modulename\Foo\Bar
  5. More documentation

Wanted: Co-Maintainers for "soft tasks"

I am happy to do all the heavy coding and thinking on this module. Or, I really enjoy it!
But it would be great to have a second person on the boat to help with documentation, testing, brainstorming, support requests, and promoting the module to a wider audience.

Project Information