Combined with the development method of the new PHP version of Composer

Source: Internet
Author: User
I. Some smelly PHP history DependencyManagerForPHP and Composer. Before the advent of Composer, PHP code was difficult to manage. Although the pear community supports many reusable code that can be obtained through pear, pear is very poor in processing code associativity, and of course there are still many problems. Java has Maven tools and. Net V

I. Dependency Manager For PHP and Composer. Before the advent of Composer, PHP code was difficult to manage. Although the pear community supports many reusable code that can be obtained through pear, pear is very poor in processing code associativity, and of course there are still many problems. Java has Maven tools and. Net V

I. Some smelly PHP history

Dependency Manager For PHP and Composer. Before the advent of Composer, PHP code was difficult to manage. Although the pear community supports many reusable code that can be obtained through pear, pear is very poor in processing code associativity, and of course there are still many problems. Maven tools are available in the Java field, And. Net VS tools integrate NuGet, which are all very useful associated managers. But where does PHP go? Composer was born. The birth of php 3.0 is largely because it is widely used. The namespace feature of php enables code packages to be unique globally. Of course, some people say that we can also write articles on the naming of a class, but it will bring many problems, such as the long class name, the high name duplication, and the poor organizational identification of files. PHP cannot ignore java at all.. net has been using the package and namespace features, and php must be replaced. With the popularity of php3.0 and the efforts of many third parties, the PHP community has accumulated a lot of available code libraries. At first, most of the Code libraries were shared on Github and obtained through git. Although this is desirable, the associated management and operability are still relatively poor. We need a simpler code library management tool with association management. Yeah. Composer.

Now, many people may be vague. It doesn't matter. We will try to get familiar with Composer.


2. Experience Composer

What is Composer? It is a compiled and compressed phar file and an executable tool.

How to get it?

Linux

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

Windows

C:\Users\username>cd C:\binC:\bin>php -r "readfile('https://getcomposer.org/installer');" | php

You can run the php composer. phar command on the console to get the help for composer.

Next we will demonstrate how to use Composer to create a PHP project.

  1. First, create a project directory ComposerDemo
  2. After entering the directory, use the preceding command to obtain composer. phar. Of course, composer can be configured globally, meaning that a Composer is shared instead of a php project.
  3. Next, call php composer. phar init to automatically create a composer. json file. Of course, you can also create it manually.

{    "name": "kendoctor/composer_demo",    "description": "introduction for how to use composer",    "minimum-stability": "stable",    "authors": [        {            "name": "kendoctor",            "email": "kendoctor@163.com"        }    ],    "require": {    }}


This file is very important and tells composer how to work. You can modify the information of your initial template.

  • Name, project name, naming rule, vendor name/Project name
  • Description, Project description
  • Minium-stability, version type. For details, refer to the official description. We will not discuss it here.
  • Authors, author information.
  • Require. You can request other related php class libraries or class library packages for your project.


Next we will demonstrate the first feature of Composer, automatic loading of Classes

First, create a file according to the directory structure

ComposerDemo/├── composer.phar├── composer.json├── src/│   ├── models│      ├── Calculator.php├── index.php

File Calculator. php
 
We need to use the Calculator class. The old php method is the require class file. In fact, we will call many classes in the code, and these classes will be put into different files. In that case, we need a lot of require.

We know that index. php can be written like this.

  AddNumbers (10, 21 );

However, this is not what we want to use Composer. The result is that a class of the instance is automatically loaded. So what should we do?

Modify Composer. json

{    "name": "kendoctor/composer_demo",    "description": "description_text",    "minimum-stability": "stable",    "authors": [        {            "name": "author's name",            "email": "email@example.com"        }    ],    "autoload":{        "classmap": ["src/"]    }}

I deleted the require attribute. Let's introduce it later. First, we introduce an autoload attribute to automatically load the feature attributes of a class or namespace. The classmap attribute defines the class under the directory to be introduced, or it can be a class file directly. All class files under the src directory are specified here. After the modification, we will use the composer command to update the contents of the directory structure, which is very simple.


php composer.php dump-autoload

After the installation is complete, the directory vendor is automatically generated, which contains many automatically generated content. However, we only need to pay attention to the file autoload. php. We only need to introduce this file in index. php.

  AddNumbers (10, 21 );

If you add another User to the models directory, you can directly call this class in the index. php file without having to do any other work.

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.