Go: Introduction to PHP Composer management tool This relatively clear point

Source: Internet
Author: User
Tags autoload naming convention pear

Transferred from: http://www.aichengxu.com/view/14872

First, some smelly history of PHP

Dependency Manager for Php,composer. PHP's code was difficult to manage before composer was born. Although Pear Community support, many reusable code can be obtained through pear, but pear is very poor in dealing with code associativity, of course, there are many problems. The Java realm has MAVEN tools, and. Net vs Tools Integrate NuGet, all of which are very good association managers. But where does PHP go? Composer was born. Its birth was largely due to the popularity of PHP's 3.0 version, and the PHP namespace feature allowed code packages to be uniquely identifiable at the global level. Of course, some people say that we can be in a class of the name of the fuss can also be done, but, will bring many problems, the class name is too long, the name of the names of the high, the organization of the identification of the file and so on. PHP has no way of ignoring the package and namespace features that Java,.net has been using, and PHP has to shake it. With php3.0
Popularity, through the efforts of a number of third parties, the PHP community has rapidly accumulated a lot of available code libraries, at first, most of the code base is shared on GitHub, in the form of Git. Although this is desirable, the correlation management and operability are still relatively poor. We need a simpler code base management tool with associated management. Yeah. Composer.

Well, when it comes to this, maybe a lot of people are still vague. It doesn't matter, we used to try to get acquainted with composer.



Second, the first experience composer

What is the specific point of composer? is a compiled compressed Phar file, a tool that can be executed.

How to get it?

Linux under

$ CURL-SS Https://getcomposer.org/installer | Php

Windows under

C:\USERS\USERNAME>CD c:\binc:\bin>php-r "ReadFile (' Https://getcomposer.org/installer ');" | Php



After that, you can get the help of composer using the command PHP Composer.phar under the console.

Let's show you how to use composer to create a PHP project



    1. First, create a project directory Composerdemo
    2. After entering the directory, use the above command to get Composer.phar, of course composer can be configured globally, meaning that you do not need a PHP project to download a composer, but a common composer
    3. The next call to PHP Composer.phar Init can automatically create a Composer.json file, but you can also create it manually.

{    "name": "Kendoctor/composer_demo",    "description": "Introduction for" and use composer ",    " Minimum-stability ":" Stable ",    " authors ": [        {            " name ":" Kendoctor ",            " email ":" [email protected] "        }    ],    "require": {    }}




This file is important and it tells composer how to work. Initially created templates, you can fix some information about your project.



    • Name, project name, naming convention, vendor name/project Name
    • Description, Project description
    • Minium-stability, version type, specific content refer to the official elaboration. There is no discussion here first.
    • Authors, author information.
    • Require, here you can request other relevant PHP class libraries or class library packages for your project




Let's demonstrate the first feature of composer, the automatic loading of classes

First, create the file according to the directory structure

composerdemo/├──composer.phar├──composer.json├──src/│   ├──models│      ├──calculator.php├──index.php



File calculator.php

<?php/** * Created by JetBrains Phpstorm. * User:kendoctor * date:14-3-19 * Time: Morning 9:39 * To change this template use File | Settings | File Templates. */class Calculator {public    function addnumbers ($x, $y)    {        return $x + $y;    }}



We want to use class calculator,php the old fashioned way is require this kind of file. In fact, we will call many classes in our code, and these classes will be put into different files, so we need a lot of require.

We know index.php can write this.

<?php/** * Created by JetBrains Phpstorm. * User:kendoctor * date:14-3-19 * Time: Morning 9:40 * To change this template use File | Settings | File Templates. */require ("src/models/calculator.php"); $calc = new Calculator (); Echo $calc->addnumbers (10,21);



But that's not what we used composer wanted. The effect we want is to instance a class, which is automatically loaded. So, what's wrong?

Modify Composer.json

{    "name": "Kendoctor/composer_demo",    "description": "Description_text",    "minimum-stability": "Stable" ,    "authors": [        {            "name": "Author's name",            "email": "[email protected]"        }    ],    "AutoLoad": {        "classmap": ["src/"]    }}



Require this attribute was deleted by me, let us introduce its features later. First, introduce a AutoLoad property that automatically loads the attribute properties of a class or namespace. The Classmap property defines the class under which directory to introduce, or directly can be a class file. Here we specify all the class files under the SRC directory. After the modification, we have to update the contents of the directory structure through the composer command, very simple



PHP composer.php Dump-autoload



It will automatically generate the directory vendor, there are many automatically generated content, but here, we only need to focus on autoload.php this file. We just need to introduce this file in index.php.

<?php/** * Created by JetBrains Phpstorm. * User:kendoctor * date:14-3-19 * Time: Morning 9:40 * To change this template use File | Settings | File Templates. */require ("vendor/autoload.php"); $calc = new Calculator (); Echo $calc->addnumbers (10,21);



If you add a new user to the models directory, then you do not need to do any other work, you can directly call this class directly in the index.php file.

Go: Introduction to PHP Composer management tool This relatively clear point

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.