Make your life easier with PHP Dependency Manager

BY Uzair Masood


5 June 2017

Reading Time: 3 minutes

Ever since I have been a PHP Backend Developer I have been using Composer as the dependency manager for my projects. Quite frankly I never realised the importance of using a dependency manager until recently when I was tasked to work on a legacy project which was initially developed almost a decade back and does not have any dependency manager.

I am sure anyone in this field reading this can relate, and will agree that manual dependency management in programming, in general, is a massive pain. PHP has a tonne of frameworks, libraries and components that you can use in your projects, known as the project dependencies. In the early days of PHP, it did not have any efficient dependency management techniques. Even if the dependencies were managed manually the developer still had to worry about the autoloaders.

Fortunately, this is no longer an issue because of the two biggest and most popular dependency managers, PEAR and COMPOSER.
PEAR is the legacy dependency manager and requires the project to have PEAR specific directory structures, which means that the author of the library has to specifically prepare it for PEAR. Secondly, PEAR installed libraries are available globally on the server, which is great if multiple projects are dependent on the same library but conflicts arise if projects hosted on the same server are dependent on different versions of the same library.

Here at Digital Noir, the majority of our PHP based projects use “Composer” as their dependency manager. Composer currently is the most commonly used dependency manager and has overcome the issues of PEAR. Composer is an excellent dependency manager for PHP. Unlike PEAR it manages dependencies at the project level and uses a “json” file called “composer.json” to list all project dependencies. Once all dependencies are listed, they can be managed using simple commands.

Composer plays a crucial role throughout a project life cycle. For
example, if your project needs to have PHPUnit installed for testing, you would tell Composer that you need PHPUnit and the specific version you want. Then you would run an install command. Composer would then go out and search for the package and version you requested and download it for you. Not only will it download your requested package, but also any other packages that your requested project requires to work. PHPUnit, as an example, uses 16~ unique packages to work. It will keep all of these dependencies in a folder named vendor which helps to keep your project nice and neat.

Basic usage of composer.json

Composer provides commands to perform different operations on the composer.json file. This obviously is the safer way to edit composer.json but it can be edited manually in your editor as well. Generally, it is located under the root of the project directory.


Composer manages all project dependencies using composer.json.

The “composer require” command adds a project dependency and if you don’t have a composer.json file, one will be created. For example, the command below adds Twig as a dependency of your project.

composer require twig/extensions:”dev-master”

Alternatively, the composer init command will guide you through creating a full composer.json file for your project. Either way, once you’ve created your composer.json file you can tell Composer to download and install your dependencies into the vendor/ directory. This also applies to projects you’ve downloaded that already provide a composer.json file:

composer install

Once, the composer’s side of things are all completed, just add the following line of code to the application main php file. This lets the application use the composer’s autoload file to load all the dependencies.

require ‘vendor/autoload.php’

I hope this gives a brief and easy to understand introduction to PHP dependency managers and their benefits, and also gives you the motivation to start using Composer if you are not using it already! It will definitely make your life a lot easier!

Like what you see?
Subscribe now to receive regular updates