PHP has seemingly countless extensions and packages. See http://www.packagist.org to see the list of packages. Naturally, you would wish to have a software to manage the extensions and packages. Wish granted! PEAR, PECL, and composer are PHP package managers.

PEAR

PEAR was the first PHP package manager. It is designed for systemwide packages so anytime you install a package through PEAR, it is installed in a central repository. The scripts and commands become available from anywhere on the command-line.

PECL

PECL manages compiled extensions to PHP.

Composer

Composer is used to list project dependencies. Composer make it very easy to list and install dependencies.

Today, the community is actively using all three managers. The choice depends on the project and people working on the project.

Installing Composer on Mac

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

To use composer, simply do

$ composer

Using Composer

  1. First create a directory
  2. Run init from inside the directory
  3. composer init will create a new composer.json file
  4. Follow instructions and answer questions

Following is what you type on command line:

$ mkdir myproject
$ cd myproject
$ composer init
Package name: enter
Description: my project
Author: Alice Bob <alice@molecularsciences.org>
Minimum stability: enter
License:  MIT
Define your dependencies
... interactively: Yes
Search for a package: markdown

Note that all this information is now saved in composer.json file. composer.lock contains information about the packages installed.

Writing code

Following example shows how to code using packages downloaded through composer:

require 'vendor/autoload.php';
$x = new \testdev\markdown\MarkdownParser();
print $transformMarkdown(file_get_contents('somefile.md'));

This code converts a markdown file to html and prints it to screen.