Paulund

Getting Started With Composer In PHP

In this tutorial we are going to learn about composer and how we can use it to develop our PHP applications. Composer is a tool that allows to manage your third party dependencies in your PHP application, it will allow you to create a list of all third party scripts and libraries and their version number. Composer will then look into the repository of the third party library and the current version number and will automatically download the library into your application. Composer needs to be told exactly what libraries it requires to install per project, it will then install these libraries into a default vendor folder. An advantage of composer is that it allows you to set dependencies on the libraries. You can also setup composer to just use defined versions or you can use a wildcard on the versions and composer will get the latest version of the library. Therefore running the update script once a day will ensure you have the latest version of the library code.

Installing Composer

Composer requires that you have PHP 5.3.2 or higher installed, when you run through the installation process there will be other PHP settings that you will need to turn on such as the php_openssl extension. Depending on the repository type you will need different things install, for example if the repository is in GIT then you will need to have GIT installed. For information on how to install composer on your local machine you can see this on the composer site. Install Composer The easiest way to install composer is by downloading it directly.


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

How To Use Composer

To use composer you need to first create the composer.json file with all the dependencies you need in your application. This will be populated with a JSON object of the dependencies that need to be included in the application. The first thing you need to know about is using the require keyword this tells composer that your application requires this package to run.


{
    "require": {
        "php": ">=5.3.33",
        "zendframework/zendframework": "2.3.*",
    }
}

This tells composer that the application needs to have PHP of 5.3.33 or higher installed and that it requires Zend Framework 2.3.*, using the wildcard will automatically update up until version 2.4. Once the composer.json file is in the directory you can install the libraries by running the commands.


php composer.phar install

Default Composer Packages

There is a massive list of third party packages that you can use by default with composer, to see if the package that you need is available to use with composer then you can browser for it on Packagist.org. Packagist## Autoloading Your Dependencies

One of my favourite favourites that you get with composer is the autoload file. Inside the vendor folder there is a created file called autoload.php, if this is included in application then you simply need to instantiate the class of the library and this file will autoload it for you.


require_once 'vendor/autoload.php';

In the composer.json file you can define the autoload method that you want your application to use. I normally go for PSR-4 as it's the nicest format for your Classes and namespaces. In your composer.json file add the following


"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {}
}

This format allows you to have classes in the namespace of \Symfony\Core\Request then composer will autoload this class in the expected location of either /Symfony/Core/Request.php or /vendor/Symfony/Core/Request.php.