Home >Backend Development >PHP Tutorial >[PHP Series] Detailed explanation of PHP components

[PHP Series] Detailed explanation of PHP components

黄舟
黄舟Original
2017-03-04 14:25:332003browse

Origin

Fengye has done PHP research and development for several years before, most of which were written under the guidance of open source frameworks. I still instinctively ask me to use a certain PHP framework to develop PHP applications. It is also because I am lazy and do not study things other than frameworks.

What I’m talking to you about today is that after I got acquainted with many PHP frameworks, I found that there were many functions that were not supported by the framework’s tools. As a last resort, I had to write them myself or look for them outside. In order to reinvent the wheel as little as possible, today Fengye brings you a more practical tool that allows you to easily and quickly find the functions you want and integrate them into your PHP application.

It is a component.

Component definition

Components are packaged codes that are used to help you solve a specific problem in your PHP application. Classes, interfaces, and properties in components are usually placed in the same namespace.

Components The role of components is very single. Don’t expect one component to solve many problems for you. Components must have a single function.

The component may be a PHP file or a class, which is very simple.

Use the right tools to do the right thing. If you have the opportunity, you still hope to build a small project that accurately solves the problem through some PHP components. Components also help to keep the code lightweight and flexible. .

Select components

We can find PHP components in http://www.php.cn/.

If you are interested in which components of PHP are better, you might as well go to this link.

http://www.php.cn/

This link lists many excellent PHP components.

If you want a component related to HTTP requests, enter HTTP in the search box above, press Enter, and you will see a list of components related to HTTP requests.

I suggest you choose the above components based on word of mouth. If you find it too troublesome, just follow the number of stars, which is more informative.

Using PHP components

Using PHP components 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 a dependency manager for PHP components and runs on the command line.

Composer can cooperate with Packagist. If you need to download components through Composer, Composer will obtain the relevant components through Packagist.

The role of Composer is very important. Dependency management and automatic loading will give you a headache. Because of the emergence of PSR-4, the dependency manager Composer will automatically generate automatic files that comply with PSR standards for all PHP components in the project. Loader. Composer solves the problem of dependency management and automatic loading.

How to install Composer

You can install it according to the official documentation: http://www.php.cn/

What I provide here is mac os and Linux. Installation method, ssh to the remote machine, and start the installation happily.

$curl -sS http://www.php.cn/ | php
$mv composer.phar /usr/local/bin/composer

If you encounter permission problems, please sudo yourself. Let's enter the composer command on the command line and see the effect.

#composer

The following screen is displayed, indicating that you have successfully installed Composer.

#If your Composer is in disrepair, it will remind you to upgrade. Simply enter the following command to complete the upgrade.

$composer self-update

Using Composer

The name of the component is generally the company name/package name. For example, in the list returned by searching for PHP in Packagist, guzzle in guzzle/http is the company name, and http is the package name. The enterprise name is globally unique. It is a global identifier and is used to identify who the package under the name belongs to. 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 under development), but we do not need to filter version by version, Composer will do this for us.

How to download this http request component? At this time, we first cd on the command line to the top-level directory of the project where we want to download the component, and enter the following command. Download the guzzle/http component.

#composer require guzzle/http

This command will cause Composer to find and install the latest stable version of the specified PHP component. In this way, you can have a PHP component related to http requests. Isn't it very simple?

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

As for the yellow part of the prompt, we will ignore it for now. This is just a component test. If you need to use the full set, it is recommended to use the following command.

#composer require guzzle/guzzle

在执行这条命令的时候,会在你项目的顶层目录里面创建两个文件:composer.json和composer.lock,记住,这两个文件都需要被纳入版本控制系统。

这个组件最终会被放在你项目顶层目录的vender/目录中。

composer.json

这个文件必须是有效的json文件,至于是否有效,大家可以拷贝上面的代码,到这个网站上面去认证:

http://www.php.cn/

它会告诉你,你的文件是不是一个标准的json。

Composer会使用这个文件中的信息对PHP组件进行查找、安装和自动加载。

composer.json文件的完整格式参见composer官网:http://www.php.cn/

composer.lock

这个文件会列出项目使用的所有PHP组件,以及组件的具体版本号,这其实和文件锁啊,进程锁啊相关的有异曲同工之妙。

为什么需要将这个文件纳入版本控制系统呢,因为你需要让你的其他团队成员知道,项目使用的PHP组件都是哪些版本的,这样能避免由于组件版本差异导致的缺陷风险。

使用组件

php组件下载下来了,如何去使用它呢,比方说我们下载下来的http组件在vender目录下面了,我们需要在我们项目的入口文件(一般是index.php)里面新增下面一句话

require 'vendor/autoload.php';

Composer下载PHP组件时还会为项目的所有依赖创建一个符合PSR标准的自动加载器。我们仅需在我们的项目入口文件内加入上面这段代码即可。这样我们就可以实例化项目中的任何PHP组件,这些组件会按需自动加载。

使用组件里面的方法与函数,一般使用下面的代码:

$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();

这样,大功告成。今天就和大伙说到这里,至于组件里面的方法如何去使用,在Packagist中都有详细的解释哒,大家可以多花点时间,多研究研究组件,会对你有很大帮助的。^_^

 以上就是【PHP系列】PHP组件详解的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn