<h2id="what-is-xautoload%3F">What is xautoload?</h2>
<p><ahref="https://www.drupal.org/project/xautoload">xautoload</a> is a Drupal module that enables the autoloading of PHP classes, in the same way that you would do so in a <ahref="http://getcomposer.org">Composer</a> based project such as Drupal 8 or Symfony.</p>
<p>It supports both the <ahref="http://www.php-fig.org/psr/psr-0/">PSR-0</a> and <ahref="http://www.php-fig.org/psr/psr-4/">PSR-4</a> standards, as well as providing a wildcard syntax for Drupal’s <code>file[]</code> syntax in .info files.</p>
<p>To use it, download and enable it from Drupal.org as you would for any other module, and then add it as a dependency within your module. The xautoload project page suggests including a minimum version in this format:</p>
<p>This will ensure that the version of xautoload is 7.x-5.0 or newer.</p>
<h2id="how-to-use-it">How to use it</h2>
<h3id="wildcard-syntax-for-.info-files">Wildcard syntax for .info files</h3>
<p>Here is an example .info file for a migrate module.</p>
<pre><codeclass="language-ini">; foo_migrate.info
name = Foo Migration
core = 7.x
package = Foo
files[] = includes/user.inc
files[] = includes/nodes/article.inc
files[] = includes/nodes/page.inc
</code></pre>
<p>In this example, each custom migration class is stored in it’s own file within the <code>includes</code> directory, and each class needs to be loaded separately using the <code>files[] = filename</code> syntax.</p>
<p>One thing that the xautoload module does to enable for the use of wildcards within this syntax. By using wildcards, the module file can be simplified as follows:</p>
<p>This will load any .inc files within the <code>includes</code> directory as well as any sub-directories, like 'node' in the original example.</p>
<p>This means that any new migration classes that are added will be automatically loaded, so you don’t need to declare each include separately within foo_migrate.info again. The great thing about this approach is that it works with the existing directory and file structure.</p>
<h3id="use-the-psr-4-structure">Use the PSR-4 structure</h3>
<p>If you want to use the <ahref="http://www.php-fig.org/psr/psr-4/">PSR-4</a> approach, you can do that too.</p>
<p>In order to do so, you’ll need to complete the following steps:</p>
<ol>
<li>Rename the <code>includes</code> directory to <code>src</code>.</li>
<li>Ensure that there is one PHP class per file, and that the file extension is <code>.php</code> rather than <code>.inc</code>.</li>
<li>Ensure that the name of the file matches the name of the class - <code>FooArticleNodeMigration</code> would be in a file called <code>FooArticleNodeMigration.php</code>.</li>
<li>Add a namespace to each PHP file. This uses the same format as Drupal 8, including the machine name of the module. For example, <code>Drupal\foo_migrate</code>.
<ul>
<li>If the class is within a sub-directory, then this will also need to be included within the namespace - e.g. <code>Drupal\foo_migrate\Node</code>.</li>
<li>You’ll also need to import any class names that you are referencing, including class names that are you extending, by adding <code>use</code> statements at the top of the file. You may be able to prefix it with <code>\</code> instead (e.g. <code>\DrupalNode6Migration</code>), but I prefer to use imports.</li>
</ul></li>
</ol>
<p>Now your class may look something like this:</p>
<pre><codeclass="language-php"><?php
namespace Drupal\foo_migrate\Node;
use DrupalNode6Migration;
class FooArticleNodeMigration extends DrupalNode6Migration {
...
}
</code></pre>
<p>With these steps completed, any imports within your .info file can be removed as they are no longer needed and any classes will be loaded automatically.</p>
<p>Within <code>foo_migrate.migrate.inc</code>, I can now reference any class names using their full namespace:</p>
<p>Oliver Davies is a Web Developer, System Administrator and Drupal specialist based in the UK. He is a Senior Developer at <ahref="https://microserve.io">Microserve</a> and also provides freelance consultancy services for Drupal websites, PHP applications and Linux servers.</p>