[PHP series] PHP component details, php component details

Source: Internet
Author: User

[PHP series] PHP component details, php component details
Origin

Feng ye has previously worked on PHP development for several years, most of which are written under the guidance of the open-source framework. Still, the instinct is to let me use a PHP framework to develop PHP applications. This is also because I am too lazy to study other things besides the framework.

Today I am talking to you about many PHP frameworks. I found that many framework tools do not support the functions. I have to write them myself or look for them outside. In order to repeat the wheel as little as possible, Feng ye today brings you a more practical tool that allows you to easily and quickly find the desired features and integrate them into your PHP application.

It is a component.

Component Definition

Components are packaged code to help you solve a specific problem in a PHP application. Classes, interfaces, and Traits in components are usually placed in the same namespace.

Component components have a very single role. Don't expect a component to solve many problems for you. components must have a single function.

A component may be a PHP file and a class, which is very simple.

Use the right tools to do the right thing. If you have the opportunity, you still want to use some PHP components to build small projects that accurately solve the problem. The components also help to make the code lightweight and flexible.

Select component

You can find the PHP Group at https://packagist.org.

If you are interested in which PHP components are good, go to this link.

Https://github.com/ziadoz/awesome-php

This link lists many excellent PHP components.

If you want an HTTP request-related component, enter HTTP in the search box above and press enter to view a list of HTTP request-related components.

I suggest selecting the above components based on Word of mouth. If it is too troublesome, we recommend that you use the number of stars for reference.

Use PHP Components

To use the PHP component, you must solve two problems: dependency management and automatic loading. Of course, we also have corresponding tools to solve it.

Composer is a tool for installing PHP components. Composer is also the dependency manager of PHP components and runs in the command line.

Composer can work with Packagist. If you need to download components through Composer, Composer will obtain related components through Packagist.

The role of Composer is very important, dependency management and automatic loading will make you a headache, because the emergence of PSR-4, the dependency manager Composer will automatically generate an automatic loader that complies with the SRS standard for all PHP components in the project. Composer solves the difficulties of dependency management and automatic loading.

How to install Composer

Everybody can install according to official documents: https://getcomposer.org/

I am here to provide installation methods for mac OS and Linux, ssh to remote machine, start a pleasant installation.

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

In case of permission issues, please perform sudo on your own. Let's enter the composer command in the command line to see the effect.

#composer

The following figure shows that you have successfully installed Composer.

If your Composer is out of service for a long time, it will remind you to upgrade. Enter the following command to complete the upgrade.

$composer self-update
Use Composer

The component name is generally the enterprise name/package name. For example, in the list returned by PHP in Packagist, guzzle in guzzle/http is the enterprise name, and http is the package name. The enterprise name is globally unique. It is a global identifier used to identify the owner of the package. The package name is used to uniquely identify a package under the enterprise name.

Packagist will list all versions of the component (including the dev version being developed). However, we do not need to filter one version or one version. Composer will help us with this.

How can we download this http request component? In this case, we first cd the command line to the top-level directory of the project where we want to download the component, run the following command to download the guzzle/http component.

#composer require guzzle/http

This command allows Composer to find and install the latest stable version of the specified PHP component. In this way, you can have an http request-related PHP component. Isn't it easy.

The following prompt is displayed, indicating that the component we want to download has been successfully downloaded!

For the yellow part of the prompt, we do not care about it for the moment. Here we just do a component test. If you need to use a full set, we suggest using the following command.

#composer require guzzle/guzzle

When executing this command, two files will be created in the top-level directory of your project: composer. json and composer. lock. Remember, both files must be included in the version control system.

This component will eventually be placed in the vender/directory in the top-level directory of your project.

Composer. json

This file must be a valid json file. As to whether the file is valid, you can copy the above Code and authenticate it on this website:

Http://www.bejson.com/

It will tell you whether your file is a standard json.

Composer will use the information in this file to find, install, and automatically load the PHP component.

For the complete format of the composer. json file, see composer Official Website: https://getcomposer.org/

Composer. lock

This file lists all the PHP components used by the project and the specific version number of the components. This is actually the same as the file lock and process lock.

Why do you need to include this file in the version control system? Because you need to let other team members know which versions of PHP components are used in the project, this avoids the risk of defects caused by component version differences.

Use Components

The php component has been downloaded. How can we use it? For example, the downloaded http component is under the vender directory. We need to file it at the entrance of our project (usually index. php) adds the following sentence

require 'vendor/autoload.php';

When the Composer downloads the PHP component, it will also create an automatic loader that complies with the PSR standard for all dependencies of the project. We only need to add the above Code in our project entry file. In this way, we can instantiate any PHP components in the project, which will be automatically loaded as needed.

The following code is generally used to use methods and functions in components:

$loop = React\EventLoop\Factory::create();$socket = new React\Socket\Server(8080, $loop);$http = new React\Http\Server($socket);$http->on('request', function (Request $request, Response $response) {    $response->writeHead(200, array('Content-Type' => 'text/plain'));    $response->end("Hello World!\n");});$loop->run();

In this way, the success is achieved. Today, I will talk to you about how to use the methods in components. I have a detailed explanation in Packagist. You can spend more time studying components, it will be of great help to you. Pai_^

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.