Software Development

Using Composer from Java Developer’s Perspective

Background

Composer is a dependency manager for PHP. It makes your life easy. If you are a Java developer and have used Apache Maven, you’ll find Composer a familiar tool. Unlike Maven, dependencies are declared in a file called composer.json which is normally placed in the root directory of a PHP project. All dependencies are, by default, downloaded into the /vendor subdirectory from https://packagist.org. In Apache Maven, files are coming from Maven Central (default).

It is also worth mentioning that the downloaded dependencies also carry with them their own composer.json file. So although you have one dependency in your composer.json file, that dependency may have dependencies on other libraries as well.

To start using the libraries, just include ‘vendor/autoload.php’ to your application’s bootstrap process. These are just a few things you’ll encounter in Composer but many of the underlying concepts and principles used are similar to that used or implemented in Maven (except the more advanced stuff).

Definitely, Composer is not Apache Maven. PHP is another world.

Software Requirements

  • Windows 7 Professional SP1
  • PHP 5.6.3 / Zend Engine v2.6.0
  • Composer v1.0-dev

Installing and Using Composer

You need to have PHP installed in your system. I will be using the PHP interpreter that comes with XAMPP.

Set Path to PHP

Open Windows command prompt and change directory to some new directory. Set the PATH to the PHP directory within your XAMPP home directory. Then, verify if PHP works.

composer001

Install Composer

Composer can be downloaded/installed using a PHP file function invoked on the command line.

composer002

Once the installation is complete, you’ll have composer.phar and that’s all you need.

composer003

Determine the Composer’s version

It is easy to find out the Composer’s version. Using composer.phar, simply pass –version as its parameter.

composer004

Your Dependency File

As mentioned before, dependencies are declared in a file named composer.json. Here, we have a sample file that declares dependency to “ollieread/multiauth”.

composer005

Then, you will have two files in your current directory.

composer006

Install the Dependencies

In Apache Maven, you would invoke “mvn install”. Here, you’ll use “php composer.phar install”.

composer007

As mentioned earlier, the libraries you reference in your composer.json may also have dependencies on other libraries. Composer will perform cascade install starting from your application’s dependency. For instance, although we referenced “ollieread/multiauth” as our direct dependency, the library also has dependencies on other libraries.

composer008

Loading

Got comments or suggestions? We disabled the comments on this site to fight off spammers, but you can still contact us via our Facebook page!.


You Might Also Like