How to declare dependencies and use composer in php

Using Composer:

We will now use Composer to install the dependencies of the project. To use Composer in your project, you’ll need one file: composer.json. In this file you will describe the dependencies of your project. So let’s create the file:

[code]vim composer.json[/code]

Declaring Dependencies:

Once you created a composer.json file within your project which describes the project’s dependencies.

"require": {
"package-name 1": "version",
"package-name 2": "version"

Installing Dependencies:

Once you updated your composer.json file with required packages, now you are able to install the mentioned packages.

To resolve and download dependencies, run the install command:

[code]$ php composer.phar install[/code]

If you did a global install and do not have the phar in that directory run this instead:

[code]$ composer install[/code]

Upgrade the dependent libraries to newer versions, you can run the following command:

[code]$ php composer.phar update[/code]

[code]$ composer update[/code]

Autoloading – Include packages into your project:

Another cool thing about Composer is its autoloading feature. For those libraries that provide autoloading information, Composer automatically generates an autoload.php file directly in the vendor folder that you can include in your project. Then you can directly start using the classes from those libraries. In your PHP project, you can just specify this:

[code]require ‘vendor/autoload.php’;[/code]

For more information: visit

Permanent link to this article:

Leave a Reply

Your email address will not be published.