Before you know the PHP life cycle, learn how Apache is associated with PHP.
Analysis of 1.Apache operation mechanism
-----------------------------
Overall as follows:
Apache hook mechanism Apache hook mechanism refers to: Apache allow modules (including internal modules and external modules, such as mod_php5.so,mod_perl.so, etc. ) Injects a custom function into the request processing loop. In other words, the module can join the Apache request processing process by hooking up its own processing function at any one of Apache's processing stages.
Mod_php5.so/php5apache2.dll is the inclusion of the custom functions, through the hook mechanism into Apache, at all stages of the Apache processing process is responsible for processing PHP requests.
Know how Apache hook to PHP, then look below the Apache transferred to PHP after a process logic.
2.PHP Running Flowchart Solution
PHP Start and end stages
PHP begins execution after two main stages: the start phase before processing the request and the end phase after the request.
2.1 SAPI Several stages of running PHP
Module initialization phase (Modules init)
That is to call each extension of the source in the Php_minit_function method initialization module, to do some of the modules required variables, memory allocation and so on.
Request initialization phase (ask Init)
The execution environment of the PHP script is initialized by invoking the method in each extended php_rinit_function after receiving the request from the client.
Execute PHP script (This step should be the familiar part of most PHP programmers, the code you write is executed here)
Request End (ask Shutdown)
At this time each extended Php_rshutdown_function method is called to clean the request site, and Ze begins to reclaim variables and memory
Close module (modules shutdown)
The Web server exits or the command-line script finishes executing exit calls the Php_mshutdown_function method in the extended source code
After a few steps: Start-Request Start-Request Close-end SAPI interface implementation completes its life cycle
2.2 Start Stage
2.2.1 Module initialization Phase Minit
the process takes place only once throughout the SAPI life cycle, such as during the entire life cycle of Apache startup or throughout the execution of a command-line program.
After starting Apache, the PHP interpreter also starts;
PHP calls the Minit method of each extension (module), which enables these extensions to switch to the available state.
This is why the new DLL module has been introduced to restart Apache. Ini
Php_minit_function (myphpextension) { //Register constants or class initialization operations return SUCCESS;}
2.2.2 Module activation Phase Rinit
This process occurs during the request phase, such as requesting a page through a URL, which is module-activated before each request (the Rinit request begins).
After the request arrives, the SAPI layer gives control to the PHP layer, and PHP initializes the environment variables required for this request to execute the script
For example, the Rinit of the session module, if the session module is enabled in PHP.ini, the $_session variable is initialized when the module's rinit is called, and the relevant content is read, and then PHP invokes all module rinit functions, called "Request initialization." ”。
At this stage the modules can also perform some related operations, the module's Rinit function and the Minit function are similar, theRinit method can be regarded as a preparatory process, and will start automatically before the program executes.
Php_rinit_function (extension_name) {/ * Initialize session variables, pre-populate variables, redefine global Variables etc * *}
2.3 End Stage
After the request has been processed, it enters the end stage, and the general script executes to the end or by calling the exit () or the Die () function, PHP will go to the end stage. Corresponding to the start phase, the end stage is divided into two links, one at the end of the request (Rshuwdown), and one at the end of the SAPI life cycle (Mshutdown).
2.3.1 After the request is over (Rshuwdown)
Once the request has been processed, the end stage is reached and PHP starts the cleanup program.
It invokes the Rshutdown method of each module sequentially.
Rshutdown is used to clear the symbol table generated by the program runtime, which is called the unset function for each variable.
Php_rshutdown_function (extension_name) {/* do memory management, unset all variables used in the last PHP call etc */}
2.3.2 Sapi end of Life cycle (Mshutdown)
Finally, all requests have been processed.
SAPI's ready to shut down.
PHP calls each extension's Mshutdown method
This is the last opportunity for each module to release memory.
(This is for SAPI such as CGI and CLI, there is no "next request", so SAPI immediately begins to shut down. )
Php_mshutdown_function (extension_name) {/* Free handlers and persistent memory etc *}
The entire PHP life cycle is over. It is important to note that the "Start first step" and "close second step" are performed only if the server does not have a request.