PHP kernel exploration: Start and end of a single request

Source: Internet
Author: User
Tags call back sapi

PHP will go through two main stages when it starts to execute:

    • The start phase before processing the request
    • The end stage after the request

The start phase has two processes:

The first procedure is the module initialization phase (MINIT), which is performed only once throughout the SAPI life cycle (for example, throughout the life cycle of Apache startup or during the entire execution of a command-line program).

The second process is the module activation phase (RINIT), which occurs during the request phase, such as requesting a page through a URL, and module activation before each request (Rinit request starts). For example, if PHP registers some extensions, it will call back the Minit function of all modules during the Minit phase. Modules can perform some initialization work at this stage, such as registering constants, defining classes used by modules, and so on.

The module implements these callback functions by following a macro when implemented:

Php_minit_function (myphpextension) {    //Register constants or class initialization operations    return SUCCESS;}

After the request arrives, PHP initializes the basic environment for executing the script, such as creating an execution environment, including a symbol table that holds the variable name and value contents of the PHP runtime, and a symbol table for all current functions and information such as classes. PHP then invokes all of the module's Rinit functions, and at this stage each module can perform some related operations, and the module's Rinit function is similar to the Minit callback function:

Php_rinit_function (myphpextension) {    ///For example, record the request start time    //And then record the end time at the end of the request. So we can record the time it takes to process the request.    return SUCCESS;}

After the request has been processed, the end stage is reached, and the general script executes to the end, or by calling the exit () or Die () function, PHP will go to the end stage. And the beginning of the corresponding, the end stage is divided into two links, one at the end of the request to deactivate the module (Rshuwdown, corresponding to Rinit), one at the end of the SAPI life cycle (Web server exit or command line script completion exit) when the module is closed (Mshutdown, Corresponds to Minit).

Php_rshutdown_function (myphpextension) {    //For example, record the request end time and write the corresponding information to the date to the file.    return SUCCESS;}

PHP kernel exploration: Start and end of a single request

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: 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.