Composer Autoloader: The Easiest Autoloading in PHP

Often, we find ourselves developing on a PHP system that does not have an autoloading mechanism built-in (cough *WordPress*). In times like these we largely just want to set up the world’s simplest autoloading for our project (plugin or theme). This way, we can move on to the fun stuff.

If you’re unfamiliar with the concept of autoloading, we have another post that describes the What, Why, and How of Namespaces and autoloading in PHP.

Autoloader Requirements

Like any other architectural decision, let’s take a moment to define some requirements for our autoloader.

  1. Simple – Any autoloader we use should be dead-simple to set up. After it’s set up, we never want to think or worry about it again. We don’t want to have to write any custom code for our autoloader, we want someone else to do it for us.
  2. Reliable – Our autoloader is not a part of the system we want to be responsible for maintaining, the autoloader should be a utility that we can rely on. Once it’s set up, we never have to think about it again. It works the same way anywhere and everywhere.

Great. That shouldn’t be too hard. And now that I’ve padded this post out enough, let’s get to the answer.

Answer: composer init

Yep. We’re going to run composer init in the directory where we want to generate the autoloader and hit `enter` until it stops asking us questions. Let’s see what that looks like:

Requirement: composer.

[pastacode lang=”bash” manual=”%24%20composer%20init%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%E2%94%80%0A%20%20Welcome%20to%20the%20Composer%20config%20generator%20%20%0A%0AThis%20command%20will%20guide%20you%20through%20creating%20your%20composer.json%20config.%0A%0APackage%20name%20(%3Cvendor%3E%2F%3Cname%3E)%20%5Bdaggerhart%2Fautoloading-easiest%5D%3A%20%0ADescription%20%5B%5D%3A%20%0AAuthor%20%5BJonathan%20Daggerhart%20%3Cjonathan%40daggerhartlab.com%3E%2C%20n%20to%20skip%5D%3A%20%0AMinimum%20Stability%20%5B%5D%3A%20%0APackage%20Type%20(e.g.%20library%2C%20project%2C%20metapackage%2C%20composer-plugin)%20%5B%5D%3A%20%0ALicense%20%5B%5D%3A%20%0A%0ADefine%20your%20dependencies.%0A%0AWould%20you%20like%20to%20define%20your%20dependencies%20(require)%20interactively%20%5Byes%5D%3F%20%0ASearch%20for%20a%20package%3A%20%0AWould%20you%20like%20to%20define%20your%20dev%20dependencies%20(require-dev)%20interactively%20%5Byes%5D%3F%20%0ASearch%20for%20a%20package%3A%20%0AAdd%20PSR-4%20autoload%20mapping%3F%20Maps%20namespace%20%22Daggerhart%5CAutoloadingEasiest%22%20to%20the%20entered%20relative%20path.%20%5Bsrc%2F%2C%20n%20to%20skip%5D%3A%20%0A%0A%7B%0A%20%20%20%20%22name%22%3A%20%22daggerhart%2Fautoloading-easiest%22%2C%0A%20%20%20%20%22autoload%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%22psr-4%22%3A%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22Daggerhart%5C%5CAutoloadingEasiest%5C%5C%22%3A%20%22src%2F%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%2C%0A%20%20%20%20%22authors%22%3A%20%5B%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%22name%22%3A%20%22Jonathan%20Daggerhart%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%22email%22%3A%20%22jonathan%40daggerhartlab.com%22%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%5D%2C%0A%20%20%20%20%22require%22%3A%20%7B%7D%0A%7D%0A%0ADo%20you%20confirm%20generation%20%5Byes%5D%3F%20%0AGenerating%20autoload%20files%0AGenerated%20autoload%20files%0APSR-4%20autoloading%20configured.%20Use%20%22namespace%20Daggerhart%5CAutoloadingEasiest%3B%22%20in%20src%2F%0AInclude%20the%20Composer%20autoloader%20with%3A%20require%20’vendor%2Fautoload.php’%3B%0A%0A” message=”composer init” highlight=”19″ provider=”manual”/]

Take a look at line 19. During the composer init process we were asked if we’d like to generate a PSR-4 autoloader based on the package name. This is the result of our secret technique to “hit `enter` until it stops asking us questions”, composer has done everything we wanted.

Composer Autoloader Results

Let’s explore the results a little bit. I’ve created an example repo for this blog post on GitHub.

Note:

  1. We have generated a composer.json file.
  2. The composer.json contains a PSR-4 autoloader statement that targets the src/ folder.
  3. We’ve generated a vendor/ folder that contains our autoloader.

And we got all of that by simply running one command.

Example using the composer autoloading

As a part of this repo, I’ve written a very simple example that shows off the autoloader.

[pastacode lang=”php” user=”daggerhart” repos=”php_examples” path_id=”autoloading-easiest/index.php” revision=”” highlight=”” lines=”” provider=”github”/]

In this example, we require the composer generated autoloader, then immediately start using it to automatically load deeply nested classes in the project.

Changing the composer generated autoloader

In the real world, we may want to control the namespaces in our project more carefully. Rather than going with the generated namespace (in this example, Daggerhart\AutoloadingEasiest\), we probably want to use something shorter and more project-specific.

After generating our autoloader it’s fairly easy to change the namespace (or add more namespaces). We just need to do two things:

  1. First, edit the autoload.psr-4 section of our composer.json file to reflect our desired namespace(s).
  2. Then, run composer dump-autoload

“Dumping” the autoloader with composer means “regenerate the autoloader according to composer.json“. After running that command, our autoloader our been updated and we can now use the new preferred namespace.

And that’s it! Hope this helps anyone who finds themselves tired of writing their own autoloader or wondering how to write an autoloader for the very first time.

Check out these other articles on using composer:

0 Thoughts

Discussion

Leave a Reply

Your email address will not be published. Required fields are marked *