您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

php7中创建扩展的方法是什么

2026/2/11 0:35:25发布13次查看
php扩展是编译库,它允许在您的php代码中使用特定的功能。今天我们就来给大家介绍一下php创建一个扩展的方式,有需要的小伙伴可以参考参考。
将实现如下功能:
<?phpecho say();?>
输出内容:
$ php ./test.php$ hello word
在扩展中实现一个say方法,调用say方法后,输出 hello word。
第一步:生成代码
php为我们提供了生成基本代码的工具 ext_skel。这个工具在php源代码的./ext目录下。
$ cd php_src/ext/$ ./ext_skel --extname=say
extname参数的值就是扩展名称。执行ext_skel命令后,这样在当前目录下会生成一个与扩展名一样的目录。
第二步,修改config.m4配置文件
config.m4的作用就是配合phpize工具生成configure文件。configure文件是用于环境检测的。检测扩展编译运行所需的环境是否满足。现在我们开始修改config.m4文件。
$ cd ./say$ vim ./config.m4
打开,config.m4文件后,你会发现这样一段文字。
dnl if your extension references something external, use with: dnl php_arg_with(say, for say support, dnl make sure that the comment is aligned: dnl [ --with-say include say support]) dnl otherwise use enable: dnl php_arg_enable(say, whether to enable say support, dnl make sure that the comment is aligned: dnl [ --enable-say enable say support])
其中,dnl 是注释符号。上面的代码说,如果你所编写的扩展如果依赖其它的扩展或者lib库,需要去掉php_arg_with相关代码的注释。否则,去掉 php_arg_enable 相关代码段的注释。我们编写的扩展不需要依赖其他的扩展和lib库。因此,我们去掉php_arg_enable前面的注释。去掉注释后的代码如下:
dnl if your extension references something external, use with: dnl php_arg_with(say, for say support, dnl make sure that the comment is aligned: dnl [ --with-say include say support]) dnl otherwise use enable: php_arg_enable(say, whether to enable say support, make sure that the comment is aligned: [ --enable-say enable say support])
第三步,代码实现
修改say.c文件。实现say方法。
找到php_function(confirm_say_compiled),在其上面增加如下代码:
php_function(confirm_say_compiled){ zend_string *strg; strg = strpprintf(0, "hello word"); return_str(strg);}
找到 php_fe(confirm_say_compiled, 在上面增加如下代码:
php_fe(say, null)
修改后的代码如下:
const zend_function_entry say_functions[] = { php_fe(say, null) /* for testing, remove later. */ php_fe(confirm_say_compiled, null) /* for testing, remove later. */ php_fe_end /* must be the last line in say_functions[] */ }; /* }}} */
第四步,编译安装
编译扩展的步骤如下:
$ phpize$ ./configure$ make && make install
修改php.ini文件,增加如下代码:
[say]extension = say.so
然后执行,php -m 命令。在输出的内容中,你会看到say字样。
第五步,调用测试
自己写一个脚本,调用say方法。看输出的内容是否符合预期。
推荐学习:php视频教程
以上就是php7中创建扩展的方法是什么的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product