Detailed explanation of PHP life cycle

小云云
Release: 2023-03-20 11:40:02
Original
1335 people have browsed it

SAPI

SAPI(Server Application Programming Interfac) is an application programming interface (API) provided by the web server to help other developers in extending the web server capabilities.
Copy after login

SAPI is an application programming interface (API) used to help other development programs extend the functionality of web servers. It abstracts external conditions and provides a set of fixed and unified interfaces for internal PHP, so that PHP's own implementation can not be affected by the external environment and maintain a certain degree of independence.
Commonly used SAPIs in PHP include cli (command line mode, single process), php-fpm, CGI, Apache...
It is a bit like the strategy mode, using the same interface, but the implementation will be slightly different.

Detailed explanation of PHP life cycle

Startup and termination of PHP

  • Starting phase before request:

    • MINIT: Module initialization phase. Initialize some extensions, constants, classes, resources, etc., all things used by PHP scripts. (Resident memory, can be used by all requests.)

    • PRINT: Module activation phase. PHP will call the RINIT function of all modules. At this stage, each module can also perform some related operations, such as initializing variables used in this request.

  • The end phase after the request:

    • RSHUTDOWN: The request for this page has been completed, or it has been given to die by the user ( exit), at this time PHP will start the recycling program to recycle the resources used by this request. This time, the RSHUTDOWN (commonly known as Request Shutdown) method of all loaded extensions will be executed. At this time, the extension can use the variable table in the kernel to do some things. Because once PHP executes all extended RSHUTDOWN methods, it will release all resources used in this request, such as all variables in the variable table, all memory requested in this request, etc.

    • MSHUTDOWN: PHP executes all extended MSHUTDOWN and releases resources.

Life cycle of PHP

1. Single-process SAPI life cycle (CLI/CGI)

Detailed explanation of PHP life cycle

2. Multi-process SAPI life cycle (Apache2)

Detailed explanation of PHP life cycle

3. Multi-threaded SAPI life cycle

Detailed explanation of PHP life cycle

4.Embed

Embed SAPI is a special sapi that allows you to call PHP/ZE in C/C++ language The function. (I don’t know much about it, I’ll take a look at it tomorrow! http://www.laruence.com/2008/09/23/539.html)

Summary

Let’s sort out the life cycle of PHP, and The entire execution process of PHP. And how to communicate with the PHP module through SAPI.
(The picture is basically taken from the reference link, standing on the shoulders of the predecessors.)

Related recommendations:

PHP source code analysis-PHP life cycle_PHP tutorial

php Variable life cycle: PHP source code analysis-PHP life cycle

PHP life cycle opcode cache What is PHP dictionary scan? How to deal with it

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

Related labels:
source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!