zoukankan      html  css  js  c++  java
  • PHP 扩展与 ZEND 引擎的整合

            PHP 扩展是对 PHP 功能的一个补充,编写完 PHP 扩展以后, ZEND 引擎需要获取到 PHP 扩展的信息,比如 phpinfo() 函数是如何列出 PHP 扩展的信息,PHP 扩展中的函数如何提供给 PHP 程序员使用,这些是开发 PHP 扩展需要了解的内容。

            这些内容并不复杂,在开发 PHP 扩展时只要愿意去了解一下相关的部分就可以了,在这里,我给出一个简单的介绍。

            PHP 扩展中负责提供信息的结构体为 zend_module_entry,该结构体的定义如下:

     1 struct _zend_module_entry {
     2     unsigned short size;
     3     unsigned int zend_api;
     4     unsigned char zend_debug;
     5     unsigned char zts;
     6     const struct _zend_ini_entry *ini_entry;
     7     const struct _zend_module_dep *deps;
     8     const char *name;
     9     const struct _zend_function_entry *functions;
    10     int (*module_startup_func)(INIT_FUNC_ARGS);
    11     int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
    12     int (*request_startup_func)(INIT_FUNC_ARGS);
    13     int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
    14     void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
    15     const char *version;
    16     size_t globals_size;
    17 #ifdef ZTS
    18     ts_rsrc_id* globals_id_ptr;
    19 #else
    20     void* globals_ptr;
    21 #endif
    22     void (*globals_ctor)(void *global);
    23     void (*globals_dtor)(void *global);
    24     int (*post_deactivate_func)(void);
    25     int module_started;
    26     unsigned char type;
    27     void *handle;
    28     int module_number;
    29     const char *build_id;
    30 };

            有了上面的结构体以后,那么 PHP 扩展的信息就已经有了,那么就可以将该结构体的信息提供给 ZEND 引擎,获取该结构体信息的函数为 get_module(),该函数的定义如下:

    1 #define ZEND_GET_MODULE(name) 
    2     BEGIN_EXTERN_C()
    3     ZEND_DLEXPORT zend_module_entry *get_module(void) { return &name##_module_entry; }
    4     END_EXTERN_C()

            get_module() 函数返回一个 zend_module_entry 结构体的指针,通过 ## 完成字符串的拼接,然后通过 & 取地址符获得结构体的内容即可。

            通过这两部分就可以完成 PHP 扩展到 ZEND 引擎的整合,不过好在 zend_module_entry 结构体会由扩展模板生成工具进行填充,而 get_module() 函数也不需要我们自己去调用,但是整合的原理还是要大体了解一下的,具体信息可以阅读相关的源码去进行理解。


    我的微信公众号:“码农UP2U”

  • 相关阅读:
    Zabbix5 Frame 嵌套
    Zabbix5 对接 SAML 协议 SSO
    CentOS7 安装 Nexus
    CentOS7 安装 SonarQube
    GitLab 后台修改用户密码
    GitLab 查看版本号
    GitLab Admin Area 500 Error
    Linux 安装 PostgreSQL
    Liger ui grid 参数
    vue.js 是一个怪东西
  • 原文地址:https://www.cnblogs.com/tosser/p/11522548.html
Copyright © 2011-2022 走看看