[module] X Autoload

HOT TIP: this content is flagged as highly recommended !


Project page quote (external)

xautoload is a comprehensive and yet highly efficient PHP class loading suite.
Most importantly, it provides Drupal-8-style PSR-0 autoloading for Drupal 7.

Required by modules [Drupal7[

I consider this module important (indeed a "god-send" for OO fans), because it enables preparation of Drupal7 modules in anticipation of the class/namespace techniques required for Drupal8. It is used by the module OOE = Object Oriented Examples = One Of Each.

As of (at least) Jun 2014, X Autoload supports the following patterns:

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

Although one is supposed to be able to change the default name space prefix from 'Drupal/' to anything else, I have not managed to get that to work yet.

Noteworthy from files[]=lib.php fails to autoload classes of different namespace?:

Yes, Drupal 7 knows nothing of namespaces unfortunately (namespaces were only introduced in PHP 5.3 and D7 is compatible with earlier versions than that).

However, life gets infinitely better with the XAutoload module, which is PSR-0 compliant an thus knows all about namespaces.

xautoload is a comprehensive and yet highly efficient PHP class loading suite. Most importantly, it provides Drupal-8-style PSR-0 autoloading for Drupal 7.
The latest release is even moving towards PSR-4 which is great news.

If you install that module you can remove all of the files[] references from your info files, as long as you follow the \Drupal\my_module namespacing pattern. Even if you don't, XAutoload has hooks to let you define your own patterns.

answered Sep 2 '13 at 9:21 by Clive

As Clive said above, the Drupal 7 core registry cannot handle namespaces, but xautoload is your solution!

The xautoload-7.x-4.x branch has PSR-0 and PSR-4 support, and it can generate classmaps by scanning a directory (and unlike core, this does support namespaces).

Now you have a number of options:

1. Stick to PSR-0, like this, with your classes in lib/Drupal/MYMODULE/.
This works just out of the box with xautoload 7.x-3.x and 7.x-4.x. You don't have to do anything but to put your classes in the right file, and they will just work.

2. Use PSR-4, like this. Since this is still pending, xautoload has not decided whether the PSR-4 base directory should be lib/ or src/ or something else. If PSR-4 gets into Drupal 8 as "lib/" then this will become the default location supported by xautoload out of the box. On the other hand, if PSR-4 won't make it into D8, then xautoload will likely make 'src' or something else the default for the PSR-4 base, to distinguish it from 'lib'.

Until then, modules need an extra line of code on top of the *.module file, to explicitly declare what should be the PSR-4 base directory. This is to avoid future BC responsibilities.

// Put this at the top of your MYMODULE.module file.
// 'lib' could as well be 'src' or 'lib-psr4' or whatever you wish.
xautoload()->registerModulePsr4(__FILE__, 'lib');

3. Put your classes in arbitrary files, and let xautoload scan these files. The result will be cached in the regular Drupal caches, so you need to flush the caches whenever you add a new class file. This is very similar to the Drupal 7 core class registry, but it does support namespaces.

 * Implements hook_xautoload()
function MYMODULE_xautoload($api) {
  // Add directories to discover php files with classes.
  // You can be creative with the wildcards.
  // '**' means to scan subdirectories of any depth.

4. Put your classes in arbitrary files, avoid namespaces, and use the D7 core registry + xautoload wildcards. Put this in your MYMODULE.info file:

files[] = includes/**/*.inc

5. Use the PHP 5.2 compatibility mode of xautoload. E.g. class MYMODULE_Foo_Bar goes into lib/Foo/Bar.php. This is supported out of the box with any version of xautoload.

Attention: You can combine this with PSR-0 (afaik), but not with PSR-4, to avoid lib getting too crowded.

answered Dec 29 '13 at 23:32 donquixote

Concerns, issues, warnings, gotchas
GOTCHA: X Autoload does not seem to work with .inc files, only .php files
affects version: 
Drupal 8 now ships with a PSR-4 autoloader with support for PSR-0 disabled.
Visit also