Autoloading For Code Organization

composer autoload

Autoloading helps us to organize our code in a clean fashion. This makes it easier to understand how all of the pieces fit together. In fact, I would go as far as to say that code organization is half the battle. With the introduction of Composer to the PHP community, we now have a way to handle code organization like never before. As we know, Composer is The Dependency Manager for PHP. In addition to dependency management, Composer also provides efficient autoloading for us. We’ll explore autoloading in this tutorial now.


In our dependency injection for beginners tutorial, we built up a suite of classes to help us win the FIFA Soccer world championship. We had a Country, some Players, a Team, and many supporting methods and properties to accomplish our goal. We actually constructed our little application in a very top down procedural style. That is to say, we had one big PHP file that kind of looked like this.

  • Class 1
  • Class 2
  • Class 3
  • Client code / Implementation of classes.

This is perfect for learning and tutorial style applications. We can very quickly view everything that is happening in one simple PHP file. If you would like to build a larger application however, this approach will begin to fail pretty quickly. Maybe you have ten or fifteen classes to account for, and many lines of code in your client to implement. One PHP file is not going to cut it in this case. The convention is actually to create one class per PHP file. From there, your client can either include the needed classes or autoload them for use. Let’s try to break our prior application into a proper organization with autoloading.


The /root directory

We can start with the root directory. In this directory, we will have our client code in the index.php file, in addition to a source directory which holds our classes. Inside the source directory are the following three files.

/root/source/Country.php

/root/source/Player.php

/root/source/Team.php

/root/index.php

In our root directory we also have our client code which looks like this.

When we try to run our program, things are not looking good!

Fatal error: Class ‘Team’ not found in C:\wamp\www\root\index.php on line 3

include the Needed Class Files

You might be thinking, well that is an easy fix. We just need to include the needed class files like so.

When we run this, everything does work just fine.

include class files

There is a better way however, and that is to make use of autoloading with Composer. Manually including files is not going to scale, and will lead to trouble down the line. Let’s see how to fix this with Composer.


Autoload With Composer

From here on out, we’ll assume you already have Composer installed. If you don’t, go ahead and install now before proceeding. With Composer installed, we can navigate to our project root using the terminal. In our case that is C:\wamp\www\root> If you’ve never used Composer before, simply type composer to see what is available to us.

We need a composer.json before anything else, so that init command looks good to us. This command will take you through the process of creating a project using a composer.json file. For the purposes of this tutorial, it will create a lot you don’t need, so once you have your composer.json, simply edit it so you only have an empty file containing the { and } characters, then run composer install which will generate the autoload.php file we need. In fact, a few things are now created in our project, let’s see.


/root

Note we now have a vendor directory in our root.
root directory

 


/root/vendor

As well as a newly created autoload.php file
autoload file

 


/root/vendor/composer

And finally, some various supporting files for Composer.
root vendor composer directory

 


Behold! The Autoload File!

Configure PSR4

We’re almost there. First we need to configure the composer.json file for PSR4 autoloading.

Once you declare your namespace, you’ll need to run composer dump-autoload in order to generate the proper autoloading files. Let’s see.

autoload_psr4.php

Include the Autoloader

Recall that the index.php file lives in the root of our project. Note that in a prior step, we simply used three include statements to make use of the three classes we need to run our application. At this point, we are no longer using multiple include statements, but we will need to include the autoloader file just one time in our client code so that it will automatically load any class we need. In addition, we will add use statements for the Team, Country, and Player classes. We can update our client code like so.

This is perfect, but when we try to run our application, we get an error of: Fatal error: Class ‘Myapp\Team’ not found in C:\wamp\www\root\index.php on line 9. This is because we had set a namespace of Myapp. If we declare a namespace via PSR4, we need to make sure to update our class files to make use of those namespaces.

Update Class Namespaces

This is super easy. All we have to do is add namespace Myapp; to the top of our Class files.

/root/source/Country.php

/root/source/Player.php

/root/source/Team.php

Now, when we run the client code, we are right back to our championship winning ways.
include class files

Composer Autoloading Summary

Composer is the best thing in the world for PHP developers. In this tutorial, we took a look at just one aspect of what makes Composer so great, and that is it’s ability to generate robust autoloading components for your application. With this newfound skill, you can better organize and scale your applications no matter how many class dependencies it might have.