Table of Contents
Origin
Component definition
Select components
Using PHP components
How to install Composer
Using Composer
composer.json
composer.lock
使用组件
Home Backend Development PHP Tutorial [PHP Series] Detailed explanation of PHP components

[PHP Series] Detailed explanation of PHP components

Mar 04, 2017 pm 02:25 PM

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
Copy after login

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

#composer
Copy after login

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
Copy after login

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
Copy after login

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
Copy after login

在执行这条命令的时候,会在你项目的顶层目录里面创建两个文件: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';
Copy after login

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();
Copy after login

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

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


Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles