• Blogs (9)
    • 📱 236 - 992 - 3846

      đź“§ jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • php PEAR and Composer

    Blogs20142014-02-22


    php PEAR and Composer

    Currently there are two major package management systems for PHP - Composer and PEAR. Which one is right for you? The answer is both.

    • Use Composer when managing dependencies for a single project.
    • Use PEAR when managing dependencies for PHP as a whole on your system.

    Some Agile Development framework, like Symfony2, laravel use Composer, while pear is used for general purpose.

    1. PEAR

    pear (http://pear2.php.net/) is a framework and distribution system for reusable PHP components. pear is short for “PHP Extension and Application Repository” and is pronounced just like the fruit. The purpose of PEAR is to provide:

    • A structured library of open-sourced code for PHP users
    • A system for code distribution and package maintenance
    • A standard style for code written in PHP
    • The PHP Foundation Classes (PFC)
    • The PHP Extension Community Library (PECL)
    • A web site, mailing lists and download mirrors to support the PHP/PEAR community

    Pear installs packages globally, which means after installing them once they are available to all projects on that server. This can be good if many projects rely on the same package with the same version but might lead to problems if version conflicts between two projects arise.

    $ pear list
    Installed packages, channel pear.php.net:
    =========================================
    Package            Version State
    Archive_Tar        1.3.7   stable
    Console_Getopt     1.2.3   stable
    MDB2               2.4.1   stable
    MDB2_Driver_mysql  1.4.1   stable
    MDB2_Driver_mysqli 1.4.1   stable
    PEAR               1.9.4   stable
    Structures_Graph   1.0.4   stable
    XML_RPC            1.5.4   stable
    XML_Util           1.2.1   stable

    2. Composer

    Composer (https://getcomposer.org/) is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you. Composer maybe borrowed from Node’s npm or Rails’ gem (RubyGems).

    Composer is not a package manager. Yes, it deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default it will never install anything globally. Thus, it is a dependency manager.

    • You have a project that depends on a number of libraries.
    • Some of those libraries depend on other libraries.
    • You declare the things you depend on.
    • Composer finds out which versions of which packages need to be installed, and installs them (meaning it downloads them into your project).

    An example of composer.json is quite like package.json:

    {
        "require": {
            "vendor/package": "1.3.2",
            "vendor/package2": "1.*",
            "vendor/package3": ">=2.0.3"
        }
    }
    //and to install dependencies(composer.json):
    $ php composer.phar install

    A good resource: PHP: The Right Way is an easy-to-read, quick reference for PHP popular coding standards, links to authoritative tutorials around the Web and what the contributors consider to be best practices at the present time.