How to use Composer for Building Dependency in PHP?

What is Composer?

Composer is a tool to build and manage dependencies of a PHP project. In a single JSON file we can specify our project’s dependencies (packages) and in just a single command it will install them in your project for you. Composer logically has two parts, A Command line application to install/update the packages and the Package repository containing all the available packages.

When do I need to use Composer?

For a simple stand alone PHP project that doesn’t have any sort of dependencies on other Libraries then you don’t need composer.
But if there are dependencies then you should use composer. Believe me, It will ease and simplify your life. So lets look when do we need composer:

  • If a PHP project depends on another Libraries
  • If those Libraries also depends on other Libraries
  • If we want to specify which version of a which Library need to be installed.

How to Install composer?

Composer comes in a single PHP file, which when executed via PHP downloads composer.phar (a PHP archive). This `composer.phar` is your composer CLI app. You can place it under your project or you can move it to `PATH` to access it globally. Lets see, how to do it:

Installing in your local project

# Go to your Project 
$ cd /path/to/your/project
$ curl -sS | php

After running those commands, you will be having `composer.phar` in your project directory. You can verify it by running:

$ php composer.phar

This will output the composer version and all the available commands.

Installing Globally

To access composer globally, just move that `composer.phar` to `/usr/bin/`.

$ sudo mv composer.phar /usr/bin/composer

Now, your composer is set globally and you can access it directly from any directory like:

$ composer --help

How to use composer?

So far, we have learned about composer and installed it successfully. Now lets see how can we use it in our Project.

First, we need to create a file called composer.json in our project. If you are from Java background then its like `pom.xml` or for Node guys its `package.json`. Similarly, composer uses `composer.json` file to define Project Dependencies, package requirements and other settings.

Lets have a look at sample `composer.json` file :

    "require": {
        "phpunit/phpunit": "4.5.1"

We can see that we have a key called `require` in the JSON. This `require` key is used to tell composer about the dependencies of the project. In the example above, we are defining that our project depends on a phpunit package. The package name consists of vendor name and the project name like `monolog/monolog` or `phpunit/phpunit` or `vendor/package`. specifying vendor name helps preventing naming clashes. `4.5.1` specifies the exact version to install. You can specify any version by `*` or like `4.5.*`. You can also use comparison operator like `>= 4.5` or a range like `4-5`.

We can define many such dependencies under require key with different-2 versions. We can search the available packages on The Packagist. It’s the main composer package repository.

So in a nutshell it looks like this:

    "require": {
        "vendor/package": "1.4.2",
        "vendor/package2": "1.*",
        "vendor/package3": ">=2.2.3"

How to install dependencies using composer?

To install the dependencies, we simply need to run `composer install` in our project directory. This command will trigger composer to read composer.json and will start downloading dependencies one by one under `vendor` directory in your project.

$ composer install
Loading composer repositories with package information
Installing dependencies (including require-dev)
  - Installing sebastian/version (1.0.5)
    Downloading: 100%         

  - Installing sebastian/global-state (1.0.0)
    Loading from cache

  - Installing sebastian/recursion-context (1.0.0)
    Loading from cache

  - Installing sebastian/exporter (1.2.0)
    Loading from cache

  - Installing sebastian/environment (1.2.2)
    Loading from cache

  - Installing sebastian/diff (1.3.0)
    Loading from cache

  - Installing sebastian/comparator (1.1.1)
    Loading from cache

  - Installing symfony/yaml (v2.6.6)
    Loading from cache

  - Installing doctrine/instantiator (1.0.4)
    Loading from cache

  - Installing phpdocumentor/reflection-docblock (2.0.4)
    Loading from cache

  - Installing phpspec/prophecy (v1.4.1)
    Downloading: 100%         

  - Installing phpunit/php-text-template (1.2.0)
    Loading from cache

  - Installing phpunit/phpunit-mock-objects (2.3.1)
    Loading from cache

  - Installing phpunit/php-timer (1.0.5)
    Loading from cache

  - Installing phpunit/php-token-stream (1.4.1)
    Loading from cache

  - Installing phpunit/php-file-iterator (1.3.4)
    Loading from cache

  - Installing phpunit/php-code-coverage (2.0.16)
    Loading from cache

  - Installing phpunit/phpunit (4.5.1)
    Loading from cache

sebastian/global-state suggests installing ext-uopz (*)
phpdocumentor/reflection-docblock suggests installing dflydev/markdown (~1.0)
phpdocumentor/reflection-docblock suggests installing erusev/parsedown (~1.0)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Writing lock file
Generating autoload files

Now you can see a `vendor` directory in your project root having phpunit installed. Composer comes with a `PSR-4` autoloader which autoloads all the dependencies in your project. You can include all your dependencies in your project by adding this line at the top of your PHP script.

include_once './vendor/autoload.php';

You might need to adjust the path depending upon the file location in which you are including the composer autoloader. You can now use the libraries installed.



You can now run your Simple Test like this:

$ phpunit simpleTest.php 

Composer will add all your dependencies in your script via the autoloader like in the example above it will add PHPUnit for you.


So we learned How composer can ease our life and can handles dependencies for us. There are lot more things to learn about composer going forward.
It’s definitly the great way to manage your project dependencies. You can ignore the `vendor` directory in your .gitigonre file (If you use git) so that you need not to commit them to repository. You can simply run `composer install` whenever you want to install all your dependencies.

We will learn how to submit a package in composer repository in further posts. Till then, Happy Coding!!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.