1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
#下载相应版本的php源码 php.net #目录描述 ext //所有扩展的目录 main //包含PHP的主要宏定义 pear //"php扩展与应用库"目录。包含pear核心文件 sapi //包含不同服务器抽象层的代码 TSRM //ZEND和PHP的"线程安全资源管理器"目录 Zend //包含Zend引擎的所有文件。所有Zend API定义和宏等 #重要头文件 main/php.h //包含绝大部分PHP宏以及PHP API定义 Zend/zend.h //包含绝大部分Zend宏以及Zend API定义 Zend/zend_API.h //包含Zend API的定义 #ext目录下执行如下命令(test7是自定义扩展名) ./ext_skel --extname=test7 cd test7 vim config.m4 #去掉--with的注释(如果你的扩展引用一些外部的东西) PHP_ARG_WITH(test7, for test7 support, [ --with-test7 Include test7 support]) #或者去掉--enable的注释(否则使用它,如果--enable编译失败,改用--with) PHP_ARG_ENABLE(test7, whether to enable test7 support, [ --enable-test7 Enable test7 support]) #接着可以编译测试了,PHP为我们提供了一个测试的扩展函数(confirm_test7_compiled) phpize ./configure --with-php-config=/usr/local/php/bin/php-config make make install vim /usr/local/php/etc/php.ini extension=test7.so /etc/init.d/php-fpm restart #编译一个测试php文件 vim test.php <?php echo confirm_test7_compiled("hello test"); #测试运行 php test.php #输出如下:扩展测试成功! #Congratulations! You have successfully modified ext/test7/config.m4. Module hello test is now compiled into PHP. #接着我们实现一个自己的扩展函数(test7.c) #实现自定义扩展函数 PHP_FUNCTION(my_ext_func) { long num1; long num2; char * s; int s_len; zend_string *strg; // lls表示参数分别为long long string(string必须附加strlen) if (zend_parse_parameters(ZEND_NUM_ARGS(), "lls", &num1, &num2, &s, &s_len) == FAILURE) { return; } strg = strpprintf(0, "%s=%d", s, num1 * num2); RETURN_STR(strg); } #注册自定义扩展函数my_ext_func const zend_function_entry test7_functions[] = { PHP_FE(confirm_test7_compiled, NULL) /* For testing, remove later. */ PHP_FE(my_ext_func, NULL) PHP_FE_END /* Must be the last line in test7_functions[] */ }; #注意代码顺序,先实现函数,再注册它 #重新make make install 重启php #修改test.php <?php echo my_ext_func(13, 14, "13*14"); #运行php test.php 输出:13*14=182 |