一、下載php源碼php
git clone https://github.com/php/php-src.git
2,建立擴展git
cd php-src/ext/ ./ext_skel --extname=php_hello
三、修改config.m4github
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
這樣一個基本的擴展就行了vim
四、Zend函數塊入口api
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[] */ };
五、Zend模塊擴展結構post
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; };
擴展中ui
/* {{{ 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 }; /* }}} */
六、運行過程spa
#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
解釋debug
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 釋放全局變量時