Composer-Managing PHP dependencies

Source: Internet
Author: User
Tags autoload php class composer install

PHP relies on the administration.

Brief introduction

Now that the scale of the software is growing, the PHP project development model and many years ago have changed a lot. Remember when you were a beginner PHP, Boblog is a good example of a PHP project's development model. At that time, PHP 5.x or more in the beginning of the popular, still have a large number of production environment using php4.x. Because of the legacy of history, OOP is not so widely popular in PHP projects. With the release of PHP5.3, the expansion of the PHP project and the development of OOP in other languages are also emerging in PHP.

Large projects cannot start from scratch, and using the resources already provided by the community can be a great convenience for the project. However, fragmented packaging and dependency processing have made it difficult to integrate two of open source projects. Although there are pear such as the official PHP-supported package management tools, but still do not have a good unified dependency management approach. Until the advent of composer.

Composer's dependency management style looks more like the Java maven. Project compilation and packaging, dependency resolution can be easily implemented. Never bother to find or update third-party libraries, or integrate them together. All this can be done to composer.

Example

First look at a similar example of HelloWorld, let us have a perceptual understanding of composer first.

How to enable composer in a PHP project is simple, just create a Composer.json file in the project root directory, which contains the following content:

{        "require": {            "monolog/monolog": "1.2.*"     }}

This allows us to add a reference to the Monolog library. Yes, it's that simple. However, you will also need to use composer to update the dependency package for you, open your shell, and switch to the project directory to execute:

composer install

At this point, composer automatically updates the specified dependent library from the Internet. You will find a vendor folder in your project folder, which is dependent on the library package.

Next, you need to reference the autoloader in your system's public portal file to automatically load the class:

require‘vendor/autoload.php‘;
Installation

The above example gives you a general idea of the use of composer. Below you will learn how to install composer in a PHP environment.
UNIX/LINUX/OSX Environment

Fortunately, installing composer with the UNIX like environment is a simple matter. You only need one line of command:

curl -sS https://getcomposer.org/installer | php

The Composer.phar file is installed for you in your current working directory, and you can run it using PHP Composer.phar. Of course, you might want to make composer a Unix command like any other, but simply add one more step:

mv composer.phar /usr/local/bin/composer

Note that on a system with restricted permissions, you may need to use the sudo command to elevate to administrator account execution.

At this point, you can use composer in the same way as in the example.
Windows environment

Composer is recommended to use the installation package for installation, it is said to download Composer-setup.exe This can be installed in Wizard mode.

For manual installation, please refer to Http://getcomposer.org/doc/00-intro.md's introduction.

Auto Load

In order to implement the PHP class with fetch and use, the namespace definition of the class recommends following certain rules. This rule can be specified by a project group. However, to make the class library uniform, the Php-fig project specifies a PHP namespace specification PSR-0, which is used by some popular PHP projects. Composer supports this specification of the Class Library auto Loader, just add the AutoLoad node to the Composer.json file:

{        "autoload": {            "psr-0": {"Acme\\": "src/"}        }}

With regard to the PSR-0 specification, there are a few important requirements:

    • Namespace specification Reference: <vendor name> (
    • Each namespace requires a top-level space, the vendor name. Used to specify the difference at the package level.
    • The path to the namespace and PHP file is one by one, and the final namespace delimiter is converted to Directory_separator
    • The file name must be the class name. php

For this specification, you can refer to http://blog.mosil.biz/2012/08/psr-0-autoloading-standard/this article. For more information on PSR-0, refer to their official website: https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md

Note that after you modify the AutoLoad, you re-use the composer install.

Note: The composer default class library is automatically loaded using the PSR-0 specification. Therefore, no additional configuration is generally required.

Find the class library you want

Composer provides a "store" for a class library, where you can immediately find the Open Source class library package you want to use, and then add them to your project. Portal: https://packagist.org/

Package your own class library

To contribute your own class library, you first need to set up packaging information for your own class library (to first ensure that your class library is managed using composer). In the Composer.json file, set:

    {        "name": "your-vendor-name/package-name",        "require": {            "php": ">=5.3.0",            "another-vendor/package": "1.*"        }    }

You can then go to https://packagist.org/to submit your class library.

Resources
  • Quick Start
  • Document
  • Package List

Composer-Managing PHP dependencies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.