PHP扩展学习–扩展生成和编译
首先使用宝塔面板安装PHP7.1
进入到ext目录
执行命令 cd /www/server/php/71/src/ext ./ext_skel --extname=bt_test 得到如下的提示 To use your new extension, you will have to execute the following steps: 1. $ cd .. 2. $ vi ext/bt_test/config.m4 3. $ ./buildconf 4. $ ./configure --[with|enable]-bt_test 5. $ make 6. $ ./sapi/cli/php -f ext/bt_test/bt_test.php 7. $ vi ext/bt_test/bt_test.c 8. $ make
生成了bt_test 文件夹。进入文件夹 生成了如下的文件
└──╼ #ll total 24 -rw-r--r-- 1 root root 4052 Jul 5 06:15 bt_test.c -rw-r--r-- 1 root root 505 Jul 5 06:15 bt_test.php -rw-r--r-- 1 root root 2112 Jul 5 06:15 config.m4 -rw-r--r-- 1 root root 359 Jul 5 06:15 config.w32 -rw-r--r-- 1 root root 8 Jul 5 06:15 CREDITS -rw-r--r-- 1 root root 0 Jul 5 06:15 EXPERIMENTAL -rw-r--r-- 1 root root 1052 Jul 5 06:15 php_bt_test.h drwxr-xr-x 2 root root 6 Jul 5 06:15 tests
执行
/www/server/php/71/bin/phpize ./configure --with-php-config=/www/server/php/71/bin/php-config make make install
生成的SO文件在 /www/server/php/71/lib/php/extensions/no-debug-non-zts-20160303/ 目录下。
php.ini 直接调用
[bt_test] extension=bt_test.so bt_filter.enable = 1
进行测试
└──╼ #/www/server/php/71/bin/php -r 'print_r(get_extension_funcs("bt_test"));' Array ( [0] => confirm_bt_test_compiled )
生成的C文件如下:
#ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_bt_test.h" /* If you declare any globals in php_bt_test.h uncomment this: ZEND_DECLARE_MODULE_GLOBALS(bt_test) */ /* True global resources - no need for thread safety here */ static int le_bt_test; /* {{{ PHP_INI */ /* Remove comments and fill if you need to have entries in php.ini PHP_INI_BEGIN() STD_PHP_INI_ENTRY("bt_test.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_bt_test_globals, bt_test_globals) STD_PHP_INI_ENTRY("bt_test.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_bt_test_globals, bt_test_globals) PHP_INI_END() */ /* }}} */ /* Remove the following function when you have successfully modified config.m4 so that your module can be compiled into PHP, it exists only for testing purposes. */ /* Every user-visible function in PHP should document itself in the source */ /* {{{ proto string confirm_bt_test_compiled(string arg) Return a string to confirm that the module is compiled in */ PHP_FUNCTION(confirm_bt_test_compiled) { char *arg = NULL; size_t arg_len, len; zend_string *strg; if (zend_parse_parameters(ZEND_NUM_ARGS(), "s", &arg, &arg_len) == FAILURE) { return; } strg = strpprintf(0, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "bt_test", arg); RETURN_STR(strg); } /* }}} */ /* The previous line is meant for vim and emacs, so it can correctly fold and unfold functions in source code. See the corresponding marks just before function definition, where the functions purpose is also documented. Please follow this convention for the convenience of others editing your code. */ /* {{{ php_bt_test_init_globals */ /* Uncomment this function if you have INI entries static void php_bt_test_init_globals(zend_bt_test_globals *bt_test_globals) { bt_test_globals->global_value = 0; bt_test_globals->global_string = NULL; } */ /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION(bt_test) { /* If you have INI entries, uncomment these lines REGISTER_INI_ENTRIES(); */ return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION(bt_test) { /* uncomment this line if you have INI entries UNREGISTER_INI_ENTRIES(); */ return SUCCESS; } /* }}} */ /* Remove if there's nothing to do at request start */ /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION(bt_test) { #if defined(COMPILE_DL_BT_TEST) && defined(ZTS) ZEND_TSRMLS_CACHE_UPDATE(); #endif return SUCCESS; } /* }}} */ /* Remove if there's nothing to do at request end */ /* {{{ PHP_RSHUTDOWN_FUNCTION */ PHP_RSHUTDOWN_FUNCTION(bt_test) { return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(bt_test) { php_info_print_table_start(); php_info_print_table_header(2, "bt_test support", "enabled"); php_info_print_table_end(); /* Remove comments if you have entries in php.ini DISPLAY_INI_ENTRIES(); */ } /* }}} */ /* {{{ bt_test_functions[] * * Every user visible function must have an entry in bt_test_functions[]. */ const zend_function_entry bt_test_functions[] = { PHP_FE(confirm_bt_test_compiled, NULL) /* For testing, remove later. */ PHP_FE_END /* Must be the last line in bt_test_functions[] */ }; /* }}} */ /* {{{ bt_test_module_entry */ zend_module_entry bt_test_module_entry = { STANDARD_MODULE_HEADER, "bt_test", bt_test_functions, PHP_MINIT(bt_test), PHP_MSHUTDOWN(bt_test), PHP_RINIT(bt_test), /* Replace with NULL if there's nothing to do at request start */ PHP_RSHUTDOWN(bt_test), /* Replace with NULL if there's nothing to do at request end */ PHP_MINFO(bt_test), PHP_BT_TEST_VERSION, STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_BT_TEST #ifdef ZTS ZEND_TSRMLS_CACHE_DEFINE() #endif ZEND_GET_MODULE(bt_test) #endif /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */
PHP为扩展提供了5个钩子函数,
PHP执行到不同阶段时回调各个扩展定义的钩子函数,
扩展可以通过这些钩子函数介入到PHP生命周期的不同阶段中去,
这些钩子函数的定义非常简单,PHP提供了对应的宏,
定义完成后只需要设置zend_module_entry对应的函数指针即可。
这几个钩子函数执行的先后顺序:module startup -> request startup -> 编译、执行 -> request shutdown -> post deactivate -> module shutdown。
参考:https://blog.csdn.net/u013756836/article/details/106721089
https://blog.csdn.net/u013756836/article/details/106688783