Helios Solutions

loading icon
Slide background
Raja Speet
Birgitte Hundhammer
Kai Dietrich
Stijn van Driessen

Composer : The dependency manager for PHP

  • April 17, 2014
  • In: PHP

Introduction

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.


Dependency management

Composer deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project.


The problem that Composer solves is this:

  • You have a project that depends on a number of libraries.
  • Some of those libraries depend on other libraries.
  • You declare the things you depend on.
  • Composer finds out which versions of which packages need to be installed, and installs them (meaning it downloads them into your project).

Declaring dependencies

Let’s say you are creating a project, and you need a library that does logging. You decide to use monolog. In order to add it to your project, all you need to do is create a composer.json file which describes the project’s dependencies.

{
    "require": {
        "monolog/monolog": "1.2.*"
    }
}

We are simply stating that our project requires some monolog/monolog package, any version beginning with 1.2.


System Requirements

Composer requires PHP 5.3.2+ to run.

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.


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 monolog package you could create the following composer.json file.

{
  "require": {
   "monolog/monolog": "1.2.*"
  }
}

To resolve and download dependencies, run the install command:

$ php composer.phar install

If you did a global install and do not have the phar in that directory run this instead:

$ composer install

Following the example above, this will download monolog into the vendor/monolog/monolog directory.


Autoloading

Besides downloading the library, Composer also prepares an autoload file that’s capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code’s bootstrap process:

require ‘vendor/autoload.php’;

That’s it, enjoy the new gained knowledge

Related Articles

PHP – The Evergreen Technology

  • May 5, 2017
  • 3 min. read time

Wondering whether to select PHP language for the upcoming software development project? It may be considered as a bad scripting […]

Things that you need to avoid doing in PHP 7

  • June 22, 2016
  • 4 Min. read

PHP 7 is nothing short of a revolution and it would certainly change the way we deliver the applications that […]

PHP language gets better with PHP 7.0. Upgrade Today!

  • December 8, 2015

A coding language is much appreciated if it allows ease in development process for coders. There are so many scripting […]

Leave Comment