Home > Article > Backend Development > PHP source code learning--life cycle_PHP tutorial
I have been using PHP for 2 years, but I only know the outside, but not the inside, and I don’t understand its meaning; while studying, remember the key points here;
The beginning of it all: SAPI interface
SAPI (server application programming interface) refers to the specific application programming interface of PHP. PHP scripts can be executed in many ways, such as through the web server, command line, or embedded in other programs; usually we use apache or Web servers such as nginx are used to test PHP scripts, or execute scripts on the command line. After the script is executed, the web server responds, and the browser displays the response information, or displays the content on the terminal.
1) Module initialization phase (MINIT), this process is only executed once during the entire sapi life cycle (such as the entire life cycle after apache is started or the entire execution process of the command line program).
2) Module activation phase (RINIT), this process occurs in the request phase. For example, if a page is requested through a URL, module activation will be performed before the request; for example, some extension modules registered by PHP will be called back in the MINIT phase. MINIT function for all modules. The module will perform some initialization work at this stage, such as registering constants, defining classes used by the module, etc.
2. Request processing: After the request arrives, PHP initializes the basic environment for executing the script, such as creating an execution environment, including a symbol table that saves the variable names and value contents during PHP running, as well as all current functions and classes and other information symbol table. Then PHP will call the RINIT function of all modules. At this stage, each module can also perform some related operations;
3. End of request: After the request is processed, it enters the end stage. Generally, when the script is executed to the end, by calling the exit() or die() function, PHP will enter the end stage; corresponding to the start stage, the end stage is also divided into two links
1) Disable module (RSHUTDOWN, corresponding to RINIT)
2) Close the module (MSHUTDOWN, corresponding to MINIT) when the sapi life cycle ends (the web server exits or the command line exits after completion)
Single-process SAPI life cycle
PHP in command line mode belongs to the single-process sapi mode. This type of request is closed after being processed once. The entire processing process only has the following links: start-request start-request close-end sapi interface implementation is completed. life cycle. The single-process sapi life cycle is as shown in the figure:
Multi-process SAPI life cycle
In addition to the command line, there is another type of web server that handles most PHP requests, such as apache. As a web server, apache will adopt multi-process mode. After apache is started, it will fork multiple self-processes. Each process The memory space of each process is independent. Each child process will go through the start and end stages. However, the start of each child process only occurs after the process forks out. Multiple requests may be processed during the entire process life cycle. The shutdown phase will only be entered after apache is shut down or the process is terminated. Between these two phases, each request will be processed. The request starts - the request is closed. The multi-process sapi life cycle is as shown in the figure.
Multi-threaded SAPI life cycle
Multi-thread mode is similar to multi-process mode, except that the request start-request closure link is repeated in parallel throughout the entire life cycle
(Please indicate the source for reprinting: Author: jmol, Weibo: http://weibo.com/yospace Blog Park: http://www.cnblogs.com/yoainet/)