Sat Mar 30 2019

What is Composer and how does it help in PHP development?

PHP composer

Composer is an application-level package manager for the PHP programming language. It provides a standard format for managing dependencies of PHP software and required libraries. Composer is strongly inspired by Node.js's "npm" and Ruby's "bundler".

Composer was developed by Nils Adermann and Jordi Boggiano, began development in April 2011 and first released it on March 1, 2012. The project's dependency solving algorithm started out as a PHP-based port of openSUSE's libzypp sat solver.

The Composer ecosystem consists of two parts - the Composer, which is a command-line utility for installing packages, and the Packagist, the default package repository.

Composer runs from the command line and installs dependencies or libraries for an application. The users can install PHP applications that are available on "Packagist" the main repository containing packages. Composer's autoload capabilities for libraries specify autoload information to ease usage of the third-party code.

Composer offers several parameters.

  • require - add the library in parameter to the file composer.json, and install it.

  • install - install all libraries from composer.json. It's the command to use to download all PHP repository dependencies.

  • update - update all libraries from composer.json, according to the allowed versions mentioned into it.

  • remove - uninstall a library and remove it from composer.json.

So, why should you use it?

Dependencies are essential for modern development. They save you time and energy. And Composer is an exceptional dependency manager for PHP. It replaces PEAR and rightfully so. PEAR requires that your project is specially prepared to work with it, where Composer gives you all the freedom you need without any special requirements. Composer installs dependencies locally, in your project structure. You can even use a specific code revision of the package when including it in your project so you are getting great flexibility. Composer packages are versioned, so you can pin down the exact version of the package you need.

Composer will manage the dependencies you require on a project by project basis. This means that Composer will pull in all the required libraries, dependencies and manage them all in one place.

Consider using frameworks that use Composer to smooth your third-party sources and make your project organized, like Laravel, Zend Framework 2 or Symfony. You will be comfortable with managing your PHP project’s dependencies with Composer.

 

Composer can be installed in two different ways.

  • Install Locally - Local installation will download composer.phar to the current directory. The drawback of this method is that you’ll always have to reference the Composer’s executable from the directory where it’s downloaded to.

  • Install Globally - Installing Composer globally is a handy way to have access to the tool from anywhere by just executing the composer command.

 

Composer is configured with a single file named composer.json located in the root directory of the project. To follow along, just create an empty directory called composer-tutorial, fire up a text editor and create an empty composer.json file in the directory.

 

Many of the most popular frameworks have already positioned themselves to use Composer, and many more individual developers are releasing code that is Composer ready from the get-go. As a PHP developer, Composer will become your best friend, and as it’s usage increases, it will become an essential part of writing PHP on a day-to-day basis.

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.