Home  >  Article  >  Backend Development  >  The beginning and end of a PHP request: MINIT and RINIT, RSHUTDOWN and MSHUTDOWN

The beginning and end of a PHP request: MINIT and RINIT, RSHUTDOWN and MSHUTDOWN

伊谢尔伦
伊谢尔伦Original
2016-11-21 16:36:012451browse

PHP will go through two main phases after it starts executing:

The beginning phase before processing the request

The end phase after the request

There are two processes in the beginning phase:

The first process is the module initialization phase (MINIT) , This process is only performed 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).

The second process is the module activation phase (RINIT), which occurs in the request phase. For example, if a page is requested through a URL, module activation (RINIT request starts) will be performed before each request. For example, if PHP registers some extension modules, the MINIT function of all modules will be called back during the MINIT stage. The module can perform some initialization work at this stage, such as registering constants, defining classes used by the module, etc.

The module can implement these callback functions through the following macros when implementing:

PHP_MINIT_FUNCTION(myphpextension)
{
    // 注册常量或者类等初始化操作
    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 saves the variable names and value contents during PHP running. , as well as the symbol table of all current functions, classes and other information. Then PHP will call the RINIT function of all modules. At this stage, each module can also perform some related operations. The RINIT function of the module is similar to the MINIT callback function:

PHP_RINIT_FUNCTION(myphpextension)
{
    // 例如记录请求开始时间
    // 随后在请求结束的时候记录结束时间。这样我们就能够记录下处理请求所花费的时间了
    return SUCCESS; 
}

After the request is processed, it enters the end stage. Generally, the script is executed until At the end or by calling the exit() or die() function, PHP will enter the end phase. Corresponding to the beginning phase, the end phase is also divided into two stages. One is to deactivate the module after the request is completed (RSHUTDOWN, corresponding to RINIT), and the other is to close the module when the SAPI life cycle ends (the web server exits or the command line script is executed and exits). (MSHUTDOWN, corresponding to MINIT).

PHP_RSHUTDOWN_FUNCTION(myphpextension)
{
    // 例如记录请求结束时间,并把相应的信息写入到日至文件中。
    return SUCCESS; 
}


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