Codementor Events

PHP Dependency Management with Composer

Published Sep 29, 2017

One of the criticism suffered by PHP in the past was lack of a dependency management tool. Before the entry of Composer there are have been attempts to create a widely adopted PHP dependency management tool without much success an example is PHPPM and pyrus which was developed to manage PEAR packages.

What is Composer?

According to the official documentation Composer is a tool for dependency management in PHP. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you. You can think of it as NPM for PHP.

Installing Composer

Installing Composer does not take much other than running this simple command from the terminal in your project directory

curl -sS https://getcomposer.org/installer | php

or

php -r "readfile('https://getcomposer.org/installer');" | php

NOTE : Windows users can download a binary from the composer’s website.

I prefer making composer available system wide so move the composer.phar file downloaded to

mv composer.phar /usr/local/bin/composer

the path may be different depending on your OS. Now to test the power of composer create a new project directory and add a file called composer.json.
This composer.json file will contain all the declaration of the dependencies we want to install in our project. Copy this sample declaration into it.

{ "name": "Name of Project", "description": "Project Description", "license": "License type e.g MIT", "keywords": ["keywords"], "version": "project version", "authors": [{ "name": "Author's Name", "email": "Author's Email", "role": "Author's Role" }], "require": { "php": ">=5.5.9", "swiftmailer/swiftmailer": "^5.4" }, "require-dev": { "symfony/var-dumper": "^3.0", "phpunit/phpunit": "~4.0" }, "autoload": { "psr-4": { "": "app/" } }, "autoload-dev": { "psr-4": { "Test\\": "test/" } } }

A quick explanation of the composer.json file, the first section of the file deals with the project meta data which is optionally. the require part contains the a list of the project dependencies while the require-dev includes dependencies used for development for example PHPUnit which you will not want to include in your production environment, we also have the autoload part which helps autoload all the dependencies so that they will be available for you in the project and lastly the autoload-dev which autoloads development dependencies.The only none optional section is th require. you can exclude the dev part and project meta information all together or run

composer install --no-dev

Also if you add any new dependency to the file after installation all you have to do is run

composer update

To update the autoloader run

composer dump-autoload -o

How to Install packages with Composer

You can search for packages to install by going to packagist.org which is the default repository for composer.Now from the terminal navigate to your project and run this command

composer install

or this if you do not have composer installed globally

php composer.phar install

what you see should be like this screen shot below.
php dependency management

All the packages will be downloaded into a directory called vendor which may or may not exist before we run the “composer install” command.

Using Installed Dependencies

To use the downloaded packages lets create a file called index.php and add the following lines.

require_once __DIR__. "/vendor/autoload.php";

what this lines does is include all the installed packages by requiring the composer autoloader.

Now lets access and the use the packages. To access a class from any of the packages installed we include the base namespace “/” if the package does not include a namespace.

php dependency management

Installing PEAR packages with Composer

By Default composer searches packagist.org for dependencies to install but you can also specify the source where you want it to download packages like. below is a composer.json sample that installs PEAR packages

{ "repositories": [{ "type": "pear", "url": "pear.php.net" }], "require": { "pear-pear/Date_HumanDiff": "*" } }

This tutorial teaches you just enough composer to be dangerous and it is by no means a definitive guide to composer, you can read the documentation or this cheat sheet to know more about it.

Happy Coding!

Discover and read more posts from Usman Irale
get started