Skip to main content

PSR-0 and Class Autoloading for Drupal 7

Although Drupal 7 has some great class autoloading features available, in the last couple of years there have been some moves in the PHP community to create some more standardised methods for autoloading. Once such move is the PSR-0 standard, central to Drupal 8, and you can use it in your Drupal 7 projects right now.

by tim.eisenhuth /

What is Autoloading?

As it is always good practice to seperate classes into individual files, you can often find that you have many files that are required in a given application. Managing which files are needed and at which point within an application can quickly become cumbersome and so Autoloading steps in to assist. Autloading allows PHP to automatically load a class that hasnt been defined yet. The benefit is that you dont have to specifically include each of your class files and they will only be loaded if they are needed. There may be a little overhead in having to find the classes in the first place, but caching results helps and autoloading will make your code easier to manage.

What is PSR-0?

PSR-0 is a standard which defines a uniform way to provide autoloading of classes. The goal is to make different frameworks more interoperable by providing a consistent/standard way to autoload classes. For the full standard see https://gist.github.com/1234504

In Drupal 8, PSR-0 is being heavily built into the core architecture (see http://drupal.org/node/1240138). The Symfony Universal Autoloader component will handle all of the autoloading in D8, however for Drupal 7 we have to rely on a little help to get this handy autoloading capability.

Putting it together

Enter the Xautoload module (http://drupal.org/project/xautoload). This module allows you to implement PSR-0 style autoloading capabilities into your D7 modules. Note: Xautoload does not attempt to implement the Symfony class loader for D7 but implements its own lightweight class loading functionality. 

The module makes heavy use of spl_autoload_register and if enabled APC cache for storing discovered classes.

To get this handy functionality working in your own module, simply make xautoload a dependency, i.e. in your module .info file, just add:

dependencies[] = xautoload

Assuming that xautoload is downloaded and in the right place, you're ready to go. 

To get started, add your namespace to your class. For example, I have an example module called “Fruit” and in this module I want to have a class Apple.php. I just need to make sure that my class has the correct namespace. For example, at the top of my class code I have:

namespace Drupal\fruit\Apple;

Within your module directory, create a new directory structure:

lib\Drupal

This is the default location that xautoload will look for your namespaced classes. So now your class will live in:

lib/Drupal/fruit/Apple.php

So, instead of having to add these classes to your info file, or having to require or include them, you can simply instantiate them using their namespace. For example:

$apple = new Drupal\fruit\Apple;

That’s all you need. Note that Namespaces also support aliasing. So for example we could create an alias like so:

use Drupal\fruit\Apple as Apple;

Which means we could then just instantiate this object with:

$apple = new Apple;

This module makes it incredibly simple to extend your module to be a fully compliant PSR-0 style OOP architecture so that you don't have to worry about specifically including your classes. 

Personally I feel like the requirement to use fully namespaced classes whilst adding extra time actually makes code easier to read as you quickly get some context around a class.

In conclusion

Consider the PSR-0 standard for your module. It's going to be a core part of the Drupal 8 architecture, it's supported by many other frameworks, and it will make it easier for other developer's to integrate and use your code. The Xautoload module for Drupal 7 greatly simplifies this by handling all of the PSR-0 compliant autoloading for you and this module does a great job of making it as simple and perfomant as possible. 

Notes

  • Namespaces were properly introduced in PHP 5.3.0, however xautoload does support a workaround for 5.2, see the module page for more information http://drupal.org/project/xautoload.
  • Make sure you're aware of any impact including a namespace may now have on existing code.
Senior Developer