Composer: Dependency Manager for PHP

Composer: Dependency Manager for PHP

A few years ago, installing PHP libraries was a nightmare. The first issue was that you either found framework agnostic code, or you found just plain old classes all bound together by a bunch of includes and requires. The next issue was that it wasn’t always straightforward to get these packages working. Sometimes you had to figure out paths relative to where you were working, or you could only find the code for CodeIgniter, but you were working in Drupal, so you had to fiddle with it.

A few years ago, however, a small group of the leading PHP frameworks and communities got together and decided to start an organization called the PHP-FIG (Framework Interop Group). This group decided that there must be at least a few things that they could standardize, so that sharing code amongst themselves would be easier, or at least code from several different frameworks would look similar and be readable by many different groups of developers.

The FIG has been successful and it’s also been a bit of a white-wash, but the important thing that came out of this is that folks really got together and decided on a great way to structure code, and how to build an autoloader to take care of the heavy work.

Fast forward a bit, and we have Composer. Composer is a nice little app that runs on Node. The idea behind Composer is that you have a central repository that contains lots of code, and you have a file in your application that defines which packages you want to include in your project. This file —  a standard JSON file —  tells Composer where to look for the code you need, and even keep it up to date. It even keeps the dependencies of those packages up to date. As an added bonus it even builds a dynamic autoloader for you to use in just one line of code. Let’s take a look how this works.


To install Composer, open a command line utility:

curl -sS | phpmv composer.phar /usr/local/bin/composer

If you are running windows, simply get the installer.

Once installed, you can use Composer directly. In your project, create a file called composer.json. Next, head over to Packagist and find a package you want to install. I’ve selected monolog for this example —  found here. Now we can add that into our composer.json file.

Here we have told Composer that we want the package monolog/monolog —  version 1.2 to 1.3. All we do now is get our terminal open in our project root directory and run

$ composer update

Then we wait. You’ll see your console doing a lot of work, and eventually it will be done. You’ll notice a new directory on you project (if it didn’t previously exist) and this is where the new code is. All you have to do now, is include the autoloader in your project, and you can use monolog. We do this as follows:

Strictly speaking, the first —  and only thing —  you need in your Composer file is the require section, as above. This simply tells Composer the version to use to keep your code up to date. But we can also tell Composer to autoload things for us. We use the autoload keyword and supply a namespace (“Awesome”) as well as the location of the root of the namespace (“src”).

The great thing here is that you can define whichever namespaces you use, as well as the location of these packages, and you can get up and running with a project in no time —  without have to figure out how to us complicated autoloaders or anything else really, and Packagist has just about everything else you need. And if it doesn’t have what you need – write a package and put it up —  it’s free. What’s more, Packagist syncs with your GitHub account, making it simple for you to keep your packages up to date.

Composer really has made dependency management in PHP simple and powerful. We haven’t had time to look at Composer in depth, but there are links to everything is this article, so feel free to check them out.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist