How to use Composer for Building Dependency in PHP?

What is Composer?

logo-composer-transparent
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

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

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/.

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

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 :

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:

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.

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.

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:

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

Summary

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 *