Home Backend Development PHP Tutorial PHP life cycle analysis

PHP life cycle analysis

May 15, 2018 pm 03:09 PM
php cycle

This article mainly introduces the PHP life cycle. Before understanding the PHP life cycle, you need to understand how Apache is related to PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

1.Analysis of Apache operating mechanism


-----------------------------





#The overall diagram is as follows:




##Apache Hook mechanism Apache’s Hook mechanism refers to: Apache allows modules (including internal modules and external Modules,

such as mod_php5.so, mod_perl.so, etc.) inject custom functions into the request processing loop. In other words, the module can hook its own processing function in any processing stage of Apache, thereby participating in Apache's request processing process.

mod_php5.so/ php5apache2.dll is to inject the included custom functions into Apache through the Hook mechanism, and is responsible for processing php requests at each stage of the Apache processing process. .

#Now that we know how apache hooks to php, let’s take a look at the process logic after apache transfers to PHP.


##2.PHP running process Illustration

PHP start and end phases

After PHP starts executing It goes through two main phases: the beginning phase before processing the request and the ending phase after the request.


##2.1 SAPI runs PHP Several stages passed

Module initialization phase (Module init)

That is to call the method in PHP_MINIT_FUNCTION in each extension source code to initialize the module, apply for some variables required by the module, allocate memory, etc.

  1. Request initialization phase (Request init)

After receiving the client’s request Call the method in PHP_RINIT_FUNCTION of each extension to initialize the execution environment of the PHP script.

  1. Execute the PHP script (this step should be familiar to most PHP programmers, and the code you write is executed here)

  2. Request Shutdown

At this time, call the PHP_RSHUTDOWN_FUNCTION method of each extension to clean up the request site, and ZE begins to recycle variables and memory

  1. Close the module (Module shutdown)

When the Web server exits or the command line script is executed, the PHP_MSHUTDOWN_FUNCTION method in the extension source code will be called



##After the following steps: Start - Request starts - Request closes - End the SAPI interface implementation to complete its life cycle


##2.2 Starting phase

2.2.1 Module initialization Phase MINIT



# during the entire SAPI life cycle (for example, after Apache starts During the entire life cycle or the entire execution process of the command line program),
this process is only performed once.

After starting Apache, the PHP interpreter also starts;

PHP calls the MINIT method of each extension (module), thereby switching these extensions to an available state.

//This is also the reason why apache must be restarted when a new dll module is introduced. php.ini



#

PHP_MINIT_FUNCTION(myphpextension)
{
    // 注册常量或者类等初始化操作
    return SUCCESS; 
}
Copy after login





##2.2.2 Module activation phase RINIT


##

该过程发生在请求阶段, 例如通过url请求某个页面,则在每次请求之前都会进行模块激活(RINIT请求开始)。

请求到达之后,SAPI层将控制权交给PHP层,PHP初始化本次请求执行脚本所需的环境变量

例如是Session模块的RINIT,如果在php.ini中启用了Session 模块,那在调用该模块的RINIT时就会初始化$_SESSION变量,并将相关内容读入; 然后PHP会调用所有模块RINIT函数,即“请求初始化”。

在这个阶段各个模块也可以执行一些相关的操作, 模块的RINIT函数和MINIT函数类似 ,RINIT方法可以看作是一个准备过程,在程序执行之前就会自动启动。



PHP_RINIT_FUNCTION(extension_name) {
      /* Initialize session variables, pre-populate variables, redefine global variables etc */
}
Copy after login






2.3结束阶段

请求处理完后就进入了结束阶段, 一般脚本执行到末尾或者通过调用exit()或者die()函数,PHP都将进入结束阶段. 和开始阶段对应,结束阶段也分为两个环节,一个在请求结束后(RSHUWDOWN),一个在SAPI生命周期结束时(MSHUTDOWN).、


2.3.1请求结束后(RSHUWDOWN)



请求处理完后就进入了结束阶段,PHP就会启动清理程序。

它会按顺序调用各个模块的RSHUTDOWN方法。

RSHUTDOWN用以清除程序运行时产生的符号表,也就是对每个变量调用unset函数。




PHP_RSHUTDOWN_FUNCTION(extension_name) {
/* Do memory management, unset all variables used in the last PHP call etc */
}
Copy after login





2.3.2 SAPI生命周期结束时(MSHUTDOWN)


最后,所有的请求都已处理完毕

SAPI也准备关闭了

PHP调用每个扩展的MSHUTDOWN方法

这时各个模块最后一次释放内存的机会。

(这个是对于CGI和CLI等SAPI,没有“下一个请求”,所以SAPI立刻开始关闭。)



PHP_MSHUTDOWN_FUNCTION(extension_name) {
	/* Free handlers and persistent memory etc */
}
Copy after login



The entire PHP life cycle is over. It should be noted that "starting the first step" and "closing the second step" will only be executed when there is no request from the server.

Related recommendations:

##thinkPHP5.0 framework application request life cycle analysis

Vue component life cycle usage method


##React How long is the life cycle of Native components

#

The above is the detailed content of PHP life cycle analysis. For more information, please follow other related articles on the PHP Chinese website!

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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

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

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.

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.

See all articles