Composer a new era of PHP dependency management

Source: Internet
Author: User

Installing composer

Composer requires PHP 5.3.2+ to run.

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

This command will be composer.phar downloaded to the current directory. PHAR (PHP compression package) is a compressed format that can be run directly under the command line.

You can use the --install-dir option to install composer to the specified directory, for example:

-sS https://getcomposer.org/installer | php -- --install-dir=bin

Of course, you can also do a global installation:

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

Create a file in the project directory composer.json that indicates dependencies, for example, your project relies on Monolog:

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

Installation dependencies are simple, just run in the project directory:

composer install  

If there is no global installation, then run:

php composer.phar install  
Auto Load

Composer provides an auto-load feature that simply includes the following line in the initialization section of your code:

require ‘vendor/autoload.php‘;  
Module Warehouse

Packagist.org is a warehouse of composer, where many well-known PHP libraries can be found. You can also submit your own work.

Advanced Features

The above describes the basic usage of composer. Composer also has some advanced features that, although not required, can often be handy for PHP development.

How to use
<?php  require __dir__. '/vendor/autoload.php ';  Use Monolog\logger;  Use Monolog\handler\streamhandler;  Use Monolog\handler\bufferhandler;  Use Monolog\errorhandler;  Use Monolog\processor\memoryusageprocessor;   $logger = new Logger (' Error_logger ');  $stream = new Streamhandler (__dir__. ') /error.log ', logger::error);  $logger->pushhandler (New Bufferhandler ($stream, logger::D Ebug, True, true));// Use Bufferhandler to set the number of logs in the same request up to 10 and write once again file  errorhandler::register ($logger);  

Require ' vendor/autoload.php '; $log = new Monolog\logger (' name '); $log->pushhandler (New Monolog\handler\ Streamhandler (' App.log ', monolog\logger::warning)); $log->addwarning (' Foo ');

Composer a new era of PHP dependency management

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.