溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

PHP自定義擴(kuò)展之生命周期的示例分析

發(fā)布時(shí)間:2021-12-20 16:31:22 來源:億速云 閱讀:123 作者:小新 欄目:編程語言

這篇文章將為大家詳細(xì)講解有關(guān)PHP自定義擴(kuò)展之生命周期的示例分析,小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,希望大家閱讀完這篇文章后可以有所收獲。

php_request_startup

這個(gè)階段和php_module_startup差不多,都是初始化工作,比php_module_startup簡(jiǎn)單很多,可以自己看下,重點(diǎn)來看下執(zhí)行階段

php_execute_script

用gdb看看調(diào)用棧,gdb ./php  

php_execute_script打斷點(diǎn),執(zhí)行,在看下調(diào)用棧,

b php_execute_script
(gdb) r test.php
bt
#0  php_execute_script (primary_file=0x7fffffffe240)
    at /www/test/php/php-7.4.3/main/main.c:2541
#1  0x00000000008bbd85 in do_cli (argc=2, argv=0x1425af0)
    at /www/test/php/php-7.4.3/sapi/cli/php_cli.c:961
#2  0x00000000008bcd2d in main (argc=2, argv=0x1425af0)
    at /www/test/php/php-7.4.3/sapi/cli/php_cli.c:1356

在調(diào)用棧可以清楚看到執(zhí)行流程,現(xiàn)在到/cli/php_cli.c文件看看做了哪些事情,

int c;
    zend_file_handle file_handle;
    int behavior = PHP_MODE_STANDARD;
    char *reflection_what = NULL;
    volatile int request_started = 0;
    volatile int exit_status = 0;
    char *php_optarg = NULL, *orig_optarg = NULL;
    int php_optind = 1, orig_optind = 1;
    char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
    char *arg_free=NULL, **arg_excp=&arg_free;
    char *script_file=NULL, *translated_path = NULL;
    int interactive=0;
    int lineno = 0;
    const char *param_error=NULL;
    int hide_argv = 0;

    zend_try {

        CG(in_compilation) = 0; /* not initialized but needed for several options */

        while ((c = php_getopt(argc, argv, OPTIONS, &php_optarg, &php_optind, 0, 2)) != -1) {
            switch (c) {

初始化變量,解析命令到/main/main.c文件看看真正的執(zhí)行階段

PHPAPI int php_execute_script(zend_file_handle *primary_file)
{
    zend_file_handle *prepend_file_p, *append_file_p;
    zend_file_handle prepend_file = {{0}, NULL, NULL, 0, 0}, append_file = {{0}, NULL, NULL, 0, 0};
#if HAVE_BROKEN_GETCWD
    volatile int old_cwd_fd = -1;
#else
    char *old_cwd;
    ALLOCA_FLAG(use_heap)
#endif
    int retval = 0;

加載要執(zhí)行的php文件,通過zend_compile_file進(jìn)行詞法分析 語法分析,生成AST,編譯成op_array,也就是指令集,
我們看下指令集,

b zend_execute
c
bt
(gdb) p *op_array
$1 = {type = 2 '\002', arg_flags = "\000\000", fn_flags = 37748736, 
  function_name = 0x0, scope = 0x0, prototype = 0x0, num_args = 0, 
  required_num_args = 0, arg_info = 0x0, cache_size = 16, last_var = 2, 
  T = 4, last = 13, opcodes = 0x7ffff5e8b000, run_time_cache__ptr = 0x0, 
  static_variables_ptr__ptr = 0x7ffff5e78358, static_variables = 0x0, 
  vars = 0x7ffff5e790f0, refcount = 0x7ffff5e85000, last_live_range = 0, 
  last_try_catch = 0, live_range = 0x0, try_catch_array = 0x0, 
  filename = 0x7ffff5e583c0, line_start = 1, line_end = 12, 
  doc_comment = 0x0, last_literal = 5, literals = 0x7ffff5e8b1a0, 
  reserved = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0}}

然后放到zend虛擬機(jī)上執(zhí)行zend_execute(op_array, retval);這這里可以看到真正執(zhí)行的是這個(gè)zend_execute_ex(execute_data);,它是一個(gè)函數(shù)指針,我們可以更換它,知道這個(gè),那么我們?cè)趯憯U(kuò)展時(shí)可以重新寫個(gè)zend_execute_ex函數(shù)替換php默認(rèn)的,我們自己的就可以做很多事情,像攔截php函數(shù),做性能監(jiān)控。

請(qǐng)求關(guān)閉階段php_request_shutdown,模塊關(guān)閉階段php_module_shutdown這兩個(gè)階段主要是做變量銷毀,現(xiàn)在我們知道了一個(gè)自定義擴(kuò)展在生命周期里的怎么執(zhí)行的。

關(guān)于“PHP自定義擴(kuò)展之生命周期的示例分析”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,使各位可以學(xué)到更多知識(shí),如果覺得文章不錯(cuò),請(qǐng)把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI