Using the Awesome Xautoload Module

I recently discovered the awesome xautoload module by Andreas Hennings (donquixote) when looking for a backport of the Drupal 8 symfony based autoloader to use on the LSD Content Staging Project.

Neil Hastings
#Development | Posted

I recently discovered the awesome xautoload module by Andreas Hennings (donquixote) when looking for a backport of the Drupal 8 symfony based autoloader to use on the LSD Content Staging Project.

From the project page:

This module implements PSR-0, PEAR, "PSR-0-NG" and "PEAR-NG" (7.x-2.x branch) autoloading for Drupal 6 and Drupal 7.

X Autoload does establish a standard for how to name your module-provided classes and where to put those class files ("PEAR-NG" in $module_dir/lib/), but it can be configured to support other patterns as well. Especially, it does support a Drupal 8 style directory structure (PSR-0 in $module_dir/lib/), if you run PHP 5.3+.

Exactly what I was looking for. The power of the module does not stop there. The xautoload module also has a plugable discovery objects. This means you can use your own standard to use on your projects without registering new spl_autoload functions.

The final feature in xautoload is the ability to use wildcards in your .info file. This will allow your info file to stay clean when you use a large number of classes. Here is an example from the xautoload project page.

For example, in views.info, the files[] stuff could be replaced with

 

1
2
3
4
5
6
7
8
; Only look at the top level.
files[] = handlers/*.inc
files[] = includes/*.inc
files[] = plugins/*.inc
; Scan the one-below-top level and deeper.
files[] = modules/*/**/*.inc
; Scan the top level and deeper.
files[] = tests/**/*.test

The xautoload module's documentation focuses on the "PEAR-NG" convention. This convention has really long class names with lots of underscores. This works if you are using php 5.2 without namespaces but as soon as you add namespaces it becomes a duplicate of what is already stated in the class name. The PSR-0 standard is what Drupal 8 will be using.

Here is a comparison of PSR-0 to PEAR-NG. I will use an example from the LSD/CSI project

PSR-0

1
2
3
namespace DrupalspsException;

class ClassLoadException extends Exception {}

PEAR-NG

1
2
3
namespace DrupalspsException;

class sps_Exception_ClassLoadException extends Exception {}

Just take a look at the difference and you can see why I choose to use PSR-0.

Using the PSR-0 standard with the xautoload module is easy. There are no hooks you need to implement, just use the Drupal namespace in a lib directory in your project root.

The file format to place your classes in to use PSR-0 with xautoload is <span class="sy0"><</span>module root<span class="sy0">>/</span>lib<span class="sy0">/</span>Drupal<span class="sy0">/<</span>module_name<span class="sy0">>/</span>your<span class="sy0">/</span>name<span class="sy0">/</span>spaces<span class="sy0">/</span>classname.php Take the example we had above. The DrupalspsExceptionClassLoadException class is in the sps<span class="sy0">/</span>lib<span class="sy0">/</span>Drupal<span class="sy0">/</span>sps<span class="sy0">/</span>Exception<span class="sy0">/</span>ClassLoadException.php.

This may seem a little verbose at first, but when you start working with fifty or so modules, it's easy to get namespace conflicts. This standard is clean and easy to use and keeps your code separated and purposeful so it's easy to maintain.

So the next project you are working on, that uses php 5.3 ( and that should be all of your project) think about using the xautoload module.

Neil Hastings