Codementor Events

Composer: PHP Dependency Management

Published Nov 16, 2017
Composer: PHP Dependency Management

Just like Makefile is a dependency manager for C/C++ programs, Composer is a dependency manager for PHP programs. It can be installed on any modern operating system as per set of instructions given on its website at https://getcomposer.org/doc/00-intro.md. In this tutorial we will learn how to make use of this dependency manager to manage classes of PHP in a project.

Suppose our project folder is hello-composer. So just create this folder inside your web server folder. For example on a Linux system, it can be done inside the public_html folder as follows:

[ccpplinux@archlinux:~]$ mkdir hello-composer
mkdir: created directory 'hello-composer'
[ccpplinux@archlinux:~]$

Then go inside that folder and create a new folder with name src to save PHP scripts defining classes as follows:

[ccpplinux@archlinux:~]$ cd hello-composer/
[ccpplinux@archlinux:hello-composer]$ mkdir src
mkdir: created directory 'src'
[ccpplinux@archlinux:hello-composer]$

Then create a file SayHello.php inside the src folder as follows:

[ccpplinux@archlinux:hello-composer]$ cd src
[ccpplinux@archlinux:src]$ ls
[ccpplinux@archlinux:src]$ nano SayHello.php

<?php

namespace HelloComposer;

class SayHello
{
public static function world()
{
return 'Hello Composer!';
}
}

?>

After saving this file, come back to project folder and execute the command composer init as follows:

[ccpplinux@archlinux:src]$ cd ..
src/
[ccpplinux@archlinux:hello-composer]$ composer init

The interaction of this command is given as follows:

======================================
Welcome to the Composer config generator

This command will guide you through creating your composer.json config.

Package name (<vendor>/<name>) [ccpplinux/hello-composer]:
Description []: Hello Composer Project
Author [, n to skip]: Pankaj Kumar ccpplinux@gmail.com
Minimum Stability []:
Package Type (e.g. library, project, metapackage, composer-plugin) []: project
License []: Open Source

Define your dependencies.

Would you like to define your dependencies (require) interactively [yes]? no
Would you like to define your dev dependencies (require-dev) interactively [yes]? no

{
"name": "ccpplinux/hello-composer",
"description": "Hello Composer Project",
"type": "project",
"license": "Open Source",
"authors": [
{
"name": "Pankaj Kumar",
"email": "ccpplinux@gmail.com"
}
],
"require": {}
}

Do you confirm generation [yes]?

Then you will notice that a new file with name composer.json has been created in the project directory. The content of this file is as follows:

[ccpplinux@archlinux:hello-composer]$ cat composer.json
{
"name": "ccpplinux/hello-composer",
"description": "Hello Composer Project",
"type": "project",
"license": "Open Source",
"authors": [
{
"name": "Pankaj Kumar",
"email": "ccpplinux@gmail.com"
}
],
"require": {}
}
[ccpplinux@archlinux:hello-composer]$

Now add a new section to this file for defining the namespace. So the content of this file will become as follows:

{
"name": "ccpplinux/hello-composer",
"description": "Hello Composer Project",
"type": "project",
"license": "Open Source",
"authors": [
{
"name": "Pankaj Kumar",
"email": "ccpplinux@gmail.com"
}
],
"require": {},
"autoload": {
"psr-4": {
"HelloComposer\": "src/"
}
}

}

Note that HelloComposer is the name of namespace which has been used in the file SayHello.php inside src folder. In this new section, psr-4 stands for PHP Standard Recommendation - 4. More details about it can be found at https://en.wikipedia.org/wiki/PHP_Standard_Recommendation.

So the namespace HelloComposer is represented by the folder src. Therefore in all PHP scripts which we define inside the src folder to define class, we need to write the statement

namespace HelloComposer;

in the beginning of the file.

Now in order to test the performance of this namespace, let us create a folder with name tests inside the project folder:

[ccpplinux@archlinux:hello-composer]$ mkdir tests
mkdir: created directory 'tests'
[ccpplinux@archlinux:hello-composer]$

Then go inside that folder and create a new file test.php as follows:

[ccpplinux@archlinux:hello-composer]$ cd tests/
[ccpplinux@archlinux:tests]$ nano test.php

<?php

require_once DIR . '/../vendor/autoload.php'; // Autoload files using Composer autoload

use HelloComposer\SayHello;

echo SayHello::world();
?>

Now go back to project folder and execute the command composer install:

[ccpplinux@archlinux:tests]$ cd ..
src/ composer.json tests/
[ccpplinux@archlinux:hello-composer]$ composer install
Loading composer repositories with package information
Updating dependencies (including require-dev)
Nothing to install or update
Generating autoload files
[ccpplinux@archlinux:hello-composer]$

You will notice that a folder with name vendor has been created with following structure:

[ccpplinux@archlinux:hello-composer]$ tree vendor/
vendor/
├── autoload.php
└── composer
├── autoload_classmap.php
├── autoload_namespaces.php
├── autoload_psr4.php
├── autoload_real.php
├── autoload_static.php
├── ClassLoader.php
├── installed.json
└── LICENSE

1 directory, 9 files
[ccpplinux@archlinux:hello-composer]$

That is it. So now run the PHP script test.php, just execute the following command:

[ccpplinux@archlinux:hello-composer]$ php tests/test.php
Hello Composer!
[ccpplinux@archlinux:hello-composer]$

As you can see the script test.php has been successfully executed and its output has been displayed on terminal.

Discover and read more posts from Pankaj Kumar
get started