Writing something in PHP was already hard. There were always compatibility issues with older server versions that did not support your new application. Lately with all the great frameworks that are out there, another problem appeared: how could you use components that rely on other libraries, how to this for each of your projects efficiently? Notice the keyword here, efficiently.

Composer is a dependency manager for your projects. What it does is that creates the dependencies and manages all of the required project libraries, through its dedicated repository packagist. It is not a package manager, so unlike PEAR you cannot access a package globally, but you have to define what you need in a composer.json file at the root of your project. That file will contain data such names and versions of each libary in json type format like this:

 

{
	"require": {
		"vendor/package": "1.3.2",
		"vendor/package2": "1.*",
		"vendor/package3": ">=2.0.3"
	}
}

 

where "vendor/package" is the name of the library we want to use. Notice the different notation on each of the example packages, the first requires a strict version number, the second any version beginning with 1 and the last any version newer than 2.0.3.

Afterwards we need to install Composer. To do that you have to run inside your project folder:

 

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

 

if you want you can add

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

to make composer installation accessible globally and execute with just "composer" instead of the "php composer.phar"

when it's done, inside in the main folder of your project, you have to run:

php composer.phar install

Composer now creates a "vendor" folder inside your project folder and downloads all the libraries you specified in composer.json file.

Finally you need to autoload the libraries into your project. Adding the next line in your index file is enough since composer automatically creates the necessary autoload.php:

require 'vendor/autoload.php';

Let's try and install some zend packages as an example. First we create our composer.json inside our application root folder. We will add the zend-mail and zend-feed packages like:

{
	"require": {
		"zendframework/zend-mail": "2.3.*@dev",
		"zendframework/zend-feed": "2.3.*@dev"
	}
}

The "@dev" here implies we want to use the latest not yet committed dev version of the packages that belongs on the 2.3 branch. While composer can handle major releases, it can't use aliases like "master" on its own. Using flags like that is something that the developer must enable when committing. So it should be expected that some libraries will not support this feature.

if we haven't done yet

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

and finally install the needed packages

php composer.phar install

Composer will download only the necessary libraries and place them in the vendor directory for our project to be able to use them.

Even the more popular open source project in PHP can be installed through composer lake the Magento Commerce Installation