Home > Backend Development > PHP Tutorial > PHP kernel-detailed explanation of graphic code of life cycle

PHP kernel-detailed explanation of graphic code of life cycle

黄舟
Release: 2023-03-06 13:10:01
Original
2224 people have browsed it

Before understanding the PHP life cycle, first understand how apache is related to php~

1.Analysis of Apache operating mechanism


##--------------------------------





#The overall diagram is as follows:




#Apache Hook mechanism Apache’s Hook mechanism refers to: Apache allows modules (including internal modules and external modules,

For example, mod_php5.so, mod_perl.so, etc. ) Inject custom functions into the request processing loop. In other words, the module can hook its own processing function in any processing stage of Apache, thereby participating in Apache's request processing process.

mod_php5.so/ php5apache2.dll is to inject the included custom functions into Apache through the Hook mechanism, and is responsible for processing php requests at each stage of the Apache processing process. .

#Now that we know how apache hooks to php, let’s take a look at the process logic after apache transfers to PHP.


##2.PHP running process Illustration

PHP start and end phases

After PHP starts executing It goes through two main phases: the beginning phase before processing the request and the ending phase after the request.

##2.1 SAPI runs PHP Several stages passed

Module initialization phase (Module init)

    That is to call the method in PHP_MINIT_FUNCTION in each extension source code to initialize the module, apply for some variables required by the module, allocate memory, etc.

    1. Request initialization phase (Request init)

    After receiving the client’s request Call the method in PHP_RINIT_FUNCTION of each extension to initialize the execution environment of the PHP script.

    1. Execute the PHP script (this step should be familiar to most PHP programmers, and the code you write is executed here)

    2. Request Shutdown

    At this time, call the PHP_RSHUTDOWN_FUNCTION method of each extension to clean up the request site, and ZE begins to recycle variables and memory

    1. Close the module (Module shutdown)

    When the Web server exits or the command line script is executed, the PHP_MSHUTDOWN_FUNCTION method in the extension source code will be called



    ##After the following steps: Start - Request starts - Request closes - End the SAPI interface implementation to complete its life cycle


    ##2.2 Starting phase

    2.2.1 Module initialization Phase MINIT

    In 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),

    this process is only performed once.

    After starting Apache, the PHP interpreter also starts;

    PHP calls the MINIT method of each extension (module), thereby switching these extensions to an available state.

    //This is also the reason why apache must be restarted when a new dll module is introduced. php.ini

    PHP_MINIT_FUNCTION(myphpextension)
    {
        // 注册常量或者类等初始化操作
        return SUCCESS; 
    }
    Copy after login




    ##2.2.2 Module activation phase RINIT

    This process occurs in the request phase, For example, if a page is requested through the url, module activation will be performed before each request (RINIT request starts ).

    After the request arrives, the SAPI layer hands over control to the PHP layer, and PHP initializes the environment variables required to execute the script for this request

    For example, it is RINIT of the Session module. If in If the Session module is enabled in php.ini, then when calling RINIT of this module, the $_SESSION variable will be initialized and the relevant content will be read in; then PHP will call the RINIT function of all modules, which is "request initialization".

    在这个阶段各个模块也可以执行一些相关的操作, 模块的RINIT函数和MINIT函数类似 ,RINIT方法可以看作是一个准备过程,在程序执行之前就会自动启动。


    PHP_RINIT_FUNCTION(extension_name) {
          /* Initialize session variables, pre-populate variables, redefine global variables etc */
    }
    Copy after login




    2.3结束阶段

    请求处理完后就进入了结束阶段, 一般脚本执行到末尾或者通过调用exit()或者die()函数,PHP都将进入结束阶段. 和开始阶段对应,结束阶段也分为两个环节,一个在请求结束后(RSHUWDOWN),一个在SAPI生命周期结束时(MSHUTDOWN).、

    2.3.1请求结束后(RSHUWDOWN)

    请求处理完后就进入了结束阶段,PHP就会启动清理程序。

    它会按顺序调用各个模块的RSHUTDOWN方法。

    RSHUTDOWN用以清除程序运行时产生的符号表,也就是对每个变量调用unset函数。

    PHP_RSHUTDOWN_FUNCTION(extension_name) {
    /* Do memory management, unset all variables used in the last PHP call etc */
    }
    Copy after login




    2.3.2 SAPI生命周期结束时(MSHUTDOWN)

    最后,所有的请求都已处理完毕

    SAPI也准备关闭了

    PHP调用每个扩展的MSHUTDOWN方法

    这时各个模块最后一次释放内存的机会。

    (这个是对于CGI和CLI等SAPI,没有“下一个请求”,所以SAPI立刻开始关闭。)


    PHP_MSHUTDOWN_FUNCTION(extension_name) {
    	/* Free handlers and persistent memory etc */
    }
    Copy after login



    整个PHP生命周期就结束了。要注意的是,只有在服务器没有请求的情况下才会执行“启动第一步”和“关闭第二步”。




    The above is the detailed content of PHP kernel-detailed explanation of graphic code of life cycle. For more information, please follow other related articles on the PHP Chinese website!

    Related labels:
    source:php.cn
    Statement of this Website
    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
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template