0

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 to other libararies 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 underlaying concepts and principles used are similar to that used or implemented in Maven (except those more advance stuffs).

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 command line.

composer002

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

composer003

Determine 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 you composer.json may also have dependencies to 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 to other libraries.

composer008

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert