Composer : The dependency manager for PHP

Introduction

Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.


Dependency management

Composer deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project.


The problem that Composer solves is this:

  • You have a project that depends on a number of libraries.
  • Some of those libraries depend on other libraries.
  • You declare the things you depend on.
  • Composer finds out which versions of which packages need to be installed, and installs them (meaning it downloads them into your project).

Declaring dependencies

Let’s say you are creating a project, and you need a library that does logging. You decide to use monolog. In order to add it to your project, all you need to do is create a composer.json file which describes the project’s dependencies.

{
    "require": {
        "monolog/monolog": "1.2.*"
    }
}

We are simply stating that our project requires some monolog/monolog package, any version beginning with 1.2.


System Requirements

Composer requires PHP 5.3.2+ to run.

Installing Composer
Installing Composer is really easy as it can all be done through the command line. I’m using OS X, but the following should be the same for any *nix operating system.

So fire up Terminal and run the following commands:

$ curl -s https://getcomposer.org/installer | php
$ sudo mv composer.phar /usr/local/bin/composer

The first command downloads the composer.phar file to your computer. The second line moves the composer.phar file in to your bin so that is accessible globally on your computer.

Now run the following command:

$ composer

If you have installed Composer successfully, you should be given a list of available commands and descriptions.


Using Composer

Now that you have Composer installed, you can use it to require some packages into your project. To make a Composer configuration file, we just need to make a JSON file in the root of the project.
For example, if you wanted to use monolog package you could create the following composer.json file.

{
  "require": {
   "monolog/monolog": "1.2.*"
  }
}

To resolve and download dependencies, run the install command:

$ php composer.phar install

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

$ composer install

Following the example above, this will download monolog into the vendor/monolog/monolog directory.


Autoloading

Besides downloading the library, Composer also prepares an autoload file that’s capable of autoloading all of the classes in any of the libraries that it downloads. To use it, just add the following line to your code’s bootstrap process:

require ‘vendor/autoload.php’;

That’s it, enjoy the new gained knowledge

Share

Recent Posts

How does LlamaIndex augment the performance and efficiency of an LLM?

The AI research landscape is currently one of the most dynamic and vibrant fields, showing no signs of slowing down…

1 month ago

Top 7 Cloud Computing Trends to Elevate your Tech Game in 2024

In the dynamic landscape of technology, cloud computing emerges as the linchpin of innovation. Did you know the cloud computing…

3 months ago

MLOps Unvеilеd: Bеyond thе Buzzword for Businеss Transformation

Did you know thе sеcrеt bеhind Ubеr's ability to connеct drivеrs and ridеrs quickly and еfficiеntly? The answer is Michaеlangеlo!…

5 months ago

Top 7 Strategies for Seamless DevOps Implementation [INFOGRAPHIC]

DevOps, the buzzword of yesteryears, is a concrete reality in forward-moving enterprises today. Organizations are actively adopting DevOps practices to…

9 months ago

How Your Business Can Leverage AI/ML in the Cloud for Competitive Advantage?

Cloud computing and Artificial Intelligence (AI) are two fundamental pillars that are driving businesses forward in numerous ways beyond the…

12 months ago

Building Your Cloud Future: A Strategic Migration Approach [INFOGRAPHIC]

Cloud computing has revolutionized the way businesses operate by providing a highly scalable, flexible, and cost-effective way to manage IT…

1 year ago