cult3

What is PHP Composer?

Jan 07, 2013

Table of contents:

  1. Installing Composer
  2. Using Composer
  3. Autoloading
  4. Conclusion

If you have ever written anything in PHP before, you have probably found that it feels like you have to keep re-inventing the wheel anytime you want to do a common task such as User Authentication, Database Management or Request Routing. PHP now has a handful of mature frameworks that have already solved all of these problems, so wouldn’t it be easier to cherry pick the bits that you needed from each framework?

If you were to start manually picking the bits you wanted from Zend, or Laravel or Symfony, then it would become very difficult to manage. Each library might also have dependencies, and so you would end up in a mess, particularly if you required other people to work on your project.

This is where Composer comes in. Composer is a dependency manager for PHP. Composer will manage the dependencies you require on a project by project basis. This means that Composer will pull in all the required libraries, dependencies and manage them all in one place.

This kind of management for dependencies in a project is not a new concept, and in fact, much of Composer is actually inspired from npm from Node.js and Bundler from Ruby.

You might also be aware of PEAR. PEAR is an established PHP package manager that has been around for years. PEAR however, has been abandoned by many PHP developers for a number of reasons. Firstly, much of the code in PEAR is out-of-date. Secondly, PEAR forces you to install packages system wide, rather than on a project-by-project basis. This means that if you already have a project that relies on a slightly older package, you are screwed. For an excellent history of PHP packages, read Packages: The Way Forward for PHP by Phil Sturgeon.

Installing Composer

Installing Composer is really easy as it can all be done through the command line. I’m using OS X, but the following should be the same for any *nix operating system.

So fire up Terminal and run the following commands:

$ curl -s https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

The first command downloads the composer.phar file to your computer. The second line moves the composer.phar file in to your bin so that is accessible globally on your computer.

Now run the following command:

$ composer

If you have installed Composer successfully, you should be given a list of available commands and descriptions.

Installing on Windows?

If you are looking to install Composer on a Windows machine, take a look at the guide on the Composer website.

Using Composer

Now that you have Composer installed, you can use it to require some packages into your project. To make a Composer configuration file, we just need to make a JSON file in the root of the project.

For example, if you wanted to use Slim Framework you could create the following composer.json file.

{
    "require": {
        "slim/slim": "2.*"
    }
}

To install Slim via Composer, you can simply run the following command through Terminal.

$ composer install

This will automatically download Slim into your project under the directory vendor/slim/slim.

See, how easy was that?

Autoloading

Now that you have all of these different packages, you need the ability to autoload them into your project. Fortunately Composer also comes with an autoload file, which is capable of autoloading any of the files in the projects that you download.

To use the Composer autoloader, simply include the following line in your project’s index or bootstrap file.

require "vendor/autoload.php";

Now you can start using the new libraries without having to worry about loading them up.

For example:

// Autoload
require "vendor/autoload.php";

// Instantiate a Slim application
$app = new \Slim\Slim();

// Define a HTTP GET route
$app->get("/hello/:name", function ($name) {
    echo "Hello, $name";
});

// Run the Slim application
$app->run();

Conclusion

Package management is clearly the right route forward for PHP. Languages such as Ruby have shown how incredibly easy it is to use packages within projects so common problems can be solved once, and you can stop wasting time as a developer by constantly reinventing the wheel.

Many of the most popular frameworks have already positioned themselves to use Composer, and many more individual developers are releasing code that is Composer ready from the get go.

As a PHP developer, Composer will become your best friend, and as it’s usage increases, it will become an essential part of writing PHP on a day-to-day basis.

By following conventions, PHP can become a much better language to use. Composer has already solved a big problem in the PHP community, so there’s really no reason why you shouldn’t start using it today.

Philip Brown

@philipbrown

© Yellow Flag Ltd 2024.