Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

Autoloading PHP classes with Composer

Sunday, October 16th, 2016 by Servage

composerComposer is a famous PHP dependency manager. It can be used to easily download and keep third-party libraries your application depends on up-to-date. This is the most popular use case for Composer, but the library has another great feature: class autoloading.

What is class Autoloading?

Before we continue, let’s make sure you have a solid grasp on the definition of autoloading. Autoloading means you can load external libraries into your own PHP files without having to manually locate the files using the include() or require() statements. If you do it this way, you would eventually end up having many include statements.

How to autoload classes

When you install a library, Composer generates an autoload.php file that can be found in the vendor directory: vendor/autoload.php. This file contains all the required code to automatically load libraries from the vendor directory. To use it in your own PHP files, all you have to do is require() this file. Doing so would look like this:

require __DIR__ . “/vendor/autoload.php”;

After you have required the autoloader file, all classes in your vendor directory become available in your own files.

Using autoloaded classes

Now that the classes are loaded, you can easily create new instances of them. For example, if you have installed the Guzzle package using composer “require guzzlehttp/guzzle”, you can create a new instance of it by using:

$client = new \GuzzleHttp\Client();

Now you may be wondering how to find out the namespace of the library you want to use. In other words, what to write after the new keyword. There are two ways to find out the proper namespace.

The easiest way is to use an IDE, such as PhpStorm, with an auto-suggest feature. If you know you want to use the Guzzle client, you can simply type “new Client” and press Enter. The IDE will find the namespace for you and import it at the top of the file using a use statement. The second option is to go to your vendor directory, open the library and go to the src or lib directory. There you can find all the files representing the classes that are available to you.

Autoloading custom classes

In addition to autoloading third-party libraries inside the vendor directory, Composer is also capable of loading your own classes. It is common practice to place your own files under a directory called src. If you want to autoload classes from that directory in the same manner as described above, you have to tell Composer where to find those classes. This can be done by creating an object called “autoload” in your composer.json file:

{ "autoload": { "psr-4": {"Acme\\": "src/" } } }

This will load all classes from the src directory and place them under the Acme namespace. Now you can create a new object from your own class with:

$object = new \Acme\Object();

Working with Composer greatly streamlines dependencies and autoloading, and is a valuable tool for every modern PHP developer.

Autoloading PHP classes with Composer, 5.0 out of 5 based on 4 ratings
Categories: Software & Webapps, Tips & Tricks


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.