码迷,mamicode.com
首页 > Web开发 > 详细

PHP扩展的基本结构

时间:2015-08-06 00:19:18      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:

1、下载php源码

git clone https://github.com/php/php-src.git

 2,创建扩展

cd php-src/ext/
./ext_skel --extname=php_hello

 3、修改config.m4

PHP_ARG_ENABLE(php_hello, whether to enable php_hello support,
    Make sure that the comment is aligned:
[  --enable-php_hello           Enable php_hello support])
if test "$PHP_PHP_HELLO" != "no"; then
    PHP_NEW_EXTENSION(php_hello, php_hello.c, $ext_shared)
fi

 这样一个基本的扩展就好了

4、Zend函数块入口

vim ./zend_API.h +35  
typedef struct _zend_function_entry {
        const char *fname;
        void (*handler)(INTERNAL_FUNCTION_PARAMETERS);
        const struct _zend_arg_info *arg_info;
        zend_uint num_args;
        zend_uint flags;
} zend_function_entry;

fname 函数名
handler 处理接口函数的指针
_zend_arg_info 函数参数

const zend_function_entry hello_functions[] = {
        PHP_FE(confirm_hello_compiled,  NULL)           /* For testing, remove later. */
        PHP_FE_END      /* Must be the last line in hello_functions[] */
};

 5、Zend模块扩展结构

typedef struct _zend_module_entry zend_module_entry;
struct _zend_module_entry {
        unsigned short size;
        unsigned int zend_api;
        unsigned char zend_debug;
        unsigned char zts;
        const struct _zend_ini_entry *ini_entry;
        const struct _zend_module_dep *deps;
        const char *name;
        const struct _zend_function_entry *functions;
        int (*module_startup_func)(INIT_FUNC_ARGS);
        int (*module_shutdown_func)(SHUTDOWN_FUNC_ARGS);
        int (*request_startup_func)(INIT_FUNC_ARGS);
        int (*request_shutdown_func)(SHUTDOWN_FUNC_ARGS);
        void (*info_func)(ZEND_MODULE_INFO_FUNC_ARGS);
        const char *version;
        size_t globals_size;
#ifdef ZTS
        ts_rsrc_id* globals_id_ptr;
#else
        void* globals_ptr;
#endif
        void (*globals_ctor)(void *global TSRMLS_DC);
        void (*globals_dtor)(void *global TSRMLS_DC);
        int (*post_deactivate_func)(void);
        int module_started;
		  unsigned char type;
        void *handle;
        int module_number;
        const char *build_id;
};

 扩展中

/* {{{ hello_module_entry
 */
zend_module_entry hello_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
        STANDARD_MODULE_HEADER,
#endif
        "hello",
        hello_functions,
        PHP_MINIT(hello),
        PHP_MSHUTDOWN(hello),
        PHP_RINIT(hello),               /* Replace with NULL if there‘s nothing to do at request start */
        PHP_RSHUTDOWN(hello),   /* Replace with NULL if there‘s nothing to do at request end */
        PHP_MINFO(hello),
#if ZEND_MODULE_API_NO >= 20010901
        PHP_HELLO_VERSION,
#endif
        STANDARD_MODULE_PROPERTIES
};
/* }}} */

 6、运行过程

#define PHP_MINIT_FUNCTION              ZEND_MODULE_STARTUP_D
#define PHP_MSHUTDOWN_FUNCTION  ZEND_MODULE_SHUTDOWN_D
#define PHP_RINIT_FUNCTION              ZEND_MODULE_ACTIVATE_D
#define PHP_RSHUTDOWN_FUNCTION  ZEND_MODULE_DEACTIVATE_D
#define PHP_MINFO_FUNCTION              ZEND_MODULE_INFO_D
#define PHP_GINIT_FUNCTION              ZEND_GINIT_FUNCTION
#define PHP_GSHUTDOWN_FUNCTION  ZEND_GSHUTDOWN_FUNCTION

#define PHP_MODULE_GLOBALS              ZEND_MODULE_GLOBALS

 解释

PHP_MINIT_FUNCTION  初始化module时运行  
PHP_MSHUTDOWN_FUNCTION  当module被卸载时运行  
PHP_RINIT_FUNCTION  当一个REQUEST请求初始化时运行  
PHP_RSHUTDOWN_FUNCTION  当一个REQUEST请求结束时运行  
PHP_MINFO_FUNCTION  这个是设置phpinfo中这个模块的信息  
PHP_GINIT_FUNCTION  初始化全局变量时  
PHP_GSHUTDOWN_FUNCTION  释放全局变量时

 

PHP扩展的基本结构

标签:

原文地址:http://www.cnblogs.com/chenpingzhao/p/4706158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!