生成擴展框架php
cd ~/php-7.0.30/ext #進入源碼包擴展目錄 ./ext_skel --extname=my_func #生成擴展基本架構
修改配置文件vim
打開配置文件 config.m4php7
dnl Otherwise use enable: dnl PHP_ARG_ENABLE(my_func, whether to enable my_func support, dnl Make sure that the comment is aligned: dnl [ --enable-my_func Enable my_func support]) #修改成: dnl Otherwise use enable: PHP_ARG_ENABLE(my_func, whether to enable my_func support, Make sure that the comment is aligned: [ --enable-my_func Enable my_func support])
dnl 是註釋符,表示當前行是註釋。這段話是說若是此擴展依賴其餘擴展,去掉PHP_ARG_WITH段的註釋符;不然去掉PHP_ARG_ENABLE段的註釋符。顯然咱們不依賴其餘擴展或lib庫,因此去掉PHP_ARG_ENABLE段的註釋符:架構
在 my_func.c 上實現函數功能框架
PHP_FUNCTION(my_func) { //zend_string *strg; //strg = strpprintf(0, "hello world."); //RETURN_STR(strg); char *arg = NULL; size_t arg_len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } strg = strpprintf(0, "Hello, my_func_print: %s", arg); RETURN_STR(strg); }
添加到編譯列表裏(my_func.c):函數
const zend_function_entry my_func_functions[] = { PHP_FE(my_func, NULL) /*添加這行*/ PHP_FE(confirm_my_func_compiled, NULL) /* For testing, remove later. */ PHP_FE_END /* Must be the last line in my_func_functions[] */ };
編譯與安裝工具
/usr/local/php7/bin/phpize ./configure --with-php-config=/usr/local/php-7.0.31/bin/php-config make && make install #vim usr/local/php7/etc/php.ini extension=my_func.so
測試測試
php -r "echo my_func('this is a function extension.');"
config.m4this
PHP_ARG_ENABLE(world, whether to enable world support, Make sure that the comment is aligned: [ --enable-world Enable hello support]) if test "$PHP_WORLD" != "no"; then AC_DEFINE(HAVE_WORLD,1,[ ]) PHP_NEW_EXTENSION(world, world.c, $ext_shared,, -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1) fi
php_world.hcode
#ifndef PHP_WORLD_H #define PHP_WORLD_H extern zend_module_entry hello_module_entry; #define phpext_hello_ptr &hello_module_entry #define PHP_WORLD_VERSION "0.1.0" #define PHP_WORLD_EXTNAME "world" #endif
world.c
#ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_world.h" PHP_FUNCTION(world) { zend_string *strg; strg = strpprintf(0, "hello world. (from world module)"); RETURN_STR(strg); } const zend_function_entry world_functions[] = { PHP_FE(world, NULL) PHP_FE_END }; zend_module_entry world_module_entry = { STANDARD_MODULE_HEADER, PHP_WORLD_EXTNAME, world_functions, NULL, NULL, NULL, NULL, NULL, PHP_WORLD_VERSION, STANDARD_MODULE_PROPERTIES }; #ifdef COMPILE_DL_WORLD #ifdef ZTS ZEND_TSRMLS_CACHE_DEFINE() #endif ZEND_GET_MODULE(world) #endif
編譯安裝: 同上