Composer as an Autoloader
Learn how to push all the burden of class autoloading to Composer.
In this lesson, we’ll learn about the Composer tool and how to use it as an autoloader. Composer is a dependency manager for PHP. It can download PHP libraries and attach them to your project. We’ll see how to do this in the next lesson. Composer has a feature-rich class autoloader that makes dependencies visible from our project source code. We can use it to load files from our project even if we don’t have any dependencies.
Getting started with Composer
First, we need to install Composer. The official Composer documentation explains how to do this in every operational system.
We can initialize the Composer configuration by running the php composer.phar init
command in the project directory. It’ll ask several questions about the project, such as the project name and author name. We can leave default values for most of the fields. Answer no when it asks to define dependencies interactively.
Try initializing the configuration in this terminal:
After running this command, we can find a new file named composer.json
in your project directory. It’s the file with the Composer configuration that looks somewhat like this:
{"name": "me/my-project","license": "proprietary","authors": [{"name": "Super Person","email": "super.person@test.test"}],"require": {}}
How to configure autoloading
It’s time to tell Composer how to autoload our classes if it hasn’t been done automatically yet. We need to add the autoload
field into the root object in the composer.json
file.
Composer supports several autoload strategies, the primary and recommended being the PSR-4. To configure it, add a psr-4
field into the autoload
project. Its value is an object with namespace prefixes as keys and base directories as values. If a single namespace prefix matches multiple base directories, we can define them as an array, for example:
{"autoload": {"psr-4": {"App\\": "","Vendor\\Namespace\\": ["src/", "test/"]}}}
We can similarly configure the PSR-0 strategy, but we typically avoid it in newer code since it’s deprecated.
If we have a directory with files that don’t match the standards, we can define it with the classmap
strategy. Composer will scan this directory and add every class to the autoloader. Directory scans slow down the autoloader generation.
Also, we’ll have to regenerate the autoloader whenever we add a new class to this kind of a directory. So, it’s better to avoid the classmap
strategy in the new code. At the same time, it helps work with old legacy code, for example:
{"autoload": {"classmap": ["src/plugins/*/lib/", "old-code/*", "Library.php"]}}
If we need to include files on every instance, we can add them using the files
strategy, for example:
{"autoload": {"files": ["src/functions.php", "src/utils.php"]}}
If we need some classes only during development but not production, we can define them in the autoload-dev
field instead of autoload
. The configuration structure looks the same.
Generating autoloader
After configuring the autoloader, regenerate it with the command php composer.phar install
. It installs missing dependencies and also generates the autoloader.
If we only want to generate the autoloader, we can do this with the command php composer.phar dump-autoload
. After running one of these commands, we’ll see the new directory named vendor
in the project directory. It’s a directory where Composer stores all the downloaded dependencies and generated autoloader files.
Hint: Don’t commit the vendor
directory to the version control system. It will bloat the repository with many files that we didn’t make and shouldn’t edit. And, it’s useless because Composer can derive all this data from the other files in this repository. If we’re using git
, this means adding the line vendor/
to the .gitignore file.
Including autoloader
There’s a autoload.php file in the
vendor` directory. Include it to use the Composer autoloader.
<?phprequire __DIR__ . '/vendor/autoload.php';// Code that used autoloaded files
It will probably be our project’s only require
expression because we don’t have to bother with autoloading anymore.
Bringing it together
Let’s look at the same diamond-shaped application from the previous lesson. This time we use the Composer autoloader.
{ "name": "php-course/hello-world", "type": "project", "license": "propietary", "require": {}, "autoload": { "psr-4": { "App\\": "src/" } } }
We could use the Greeter
class without requiring the file containing it.
Conclusion
Composer has a built-in class autoloader. It’s so handy that we can use it even if we have no dependencies.
Composer supports multiple autoloading strategies. The recommended one is PSR-4. In practice, we add our source code directory into the composer.json
configuration file, include the Composer autoloader, and never bother with class loading anymore.