PHP Kernel-Lifecycle ____php

Source: Internet
Author: User
Learn about how Apache is associated with PHP before you know the PHP lifecycle.
analysis of 1.Apache operation mechanism
-----------------------------



The overall schematic is as follows:

Apache hook mechanism Apache hook mechanism refers to: Apache allows modules (including internal modules and external modules, such as mod_php5.so,mod_perl.so, etc.) to inject custom functions into the request processing loop. In other words, the module can hook up its own processing function at any one of the Apache processing stages to participate in the Apache request processing process. Mod_php5.so/php5apache2.dll is the inclusion of custom functions that are injected into Apache through the hook mechanism and are responsible for processing PHP requests at various stages of the Apache processing process. knowing how Apache hooks into PHP, look at the following Apache transfer to PHP after a process logic.
2.PHP Operation Flowchart SolutionPHP begins and ends after PHP begins execution with two main phases: the start stage before the request and the end stage after the request.
2.1 Sapi running PHP in several stages of the module initialization phase (module Init) that calls each extension of the source in the php_minit_function of the method initialization module, some modules required variable application, memory allocation and so on. The request initialization phase (Request init), which receives a request to the client, invokes the method in each extended php_rinit_function to initialize the execution environment of the PHP script. Execute the PHP script (this step, which should be familiar to most PHP programmers, where the code you write is executed here) the end of the request (request Shutdown) calls each extended Php_rshutdown_function method to clean up the request scene, And ze begins to recycle variables and memory shutdown module (module shutdown) Web server exit or command line script execution complete exit will invoke the Php_mshutdown_function method in the extended source code

through the following links: Start-Request Start-Request shutdown-end SAPI interface implementation to complete its lifecycle
2.2 Starting Phase2.2.1 Module initialization Phase Minit
This process is performed only once throughout the SAPI lifecycle, such as during the entire lifecycle of Apache startup or the entire execution of a command-line program. When you start Apache, the PHP interpreter starts, and PHP invokes the Minit method of each extension (module), which switches the extensions to the available state. This is why you have to restart Apache when you introduce a new DLL module. Ini

Php_minit_function (myphpextension)
{
    //Registration 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, and the module activation (RINIT request begins) before each request. 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, such as the Rinit of the session module, which, if the session module is enabled in PHP.ini, is initialized when the rinit of the module is invoked $_  Session variable and reads the relevant content into it, and then PHP calls all module Rinit functions, called initialization. In this phase, each module can also perform some related operations, the module's Rinit function and the Minit function similar, rinit method can be considered as a preparation process, before the execution of the program will automatically start.
Php_rinit_function (extension_name) {/
      * Initialize session variables, pre-populate variables, redefine global Variables etc */
}



2.3 End StageAfter 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 enter the end stage. And the start phase corresponds to the end stage is also divided into two links, one at the end of the request (Rshuwdown), one at the end of the SAPI Lifecycle (mshutdown).
After the end of the 2.3.1 request (Rshuwdown)


As soon as the request is finished, PHP starts the cleanup process and enters the end stage.  It invokes the Rshutdown method of each module in order. Rshutdown is used to clear the symbol table that is generated when the program is run, that is, to call the unset function on each variable.


Php_rshutdown_function (extension_name) {/
* do memory management, unset all variables used at the last PHP call etc * *
}




2.3.2 SAPI at the end of the life cycle (Mshutdown)
Finally, all the requests have been processed SAPI is also ready to close the PHP call to each extension of the Mshutdown method at this time each module to release the memory of the last chance. (This is for CGI and CLI and so on SAPI, there is no "next request", so SAPI immediately start shutting down. )
Php_mshutdown_function (extension_name) {/
	* Free handlers and persistent memory etc */
}



The entire PHP lifecycle is over. Note that the start first step and close second step are performed only if the server does not request it.
Original address: http://blog.csdn.net/ty_hf/article/details/52877759


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.