php

位置:IT落伍者 >> php >> 浏览文章

Linux下C语言对PHP扩展


发布日期:2022年08月31日
 
Linux下C语言对PHP扩展

搭建php环境

下载php 源码 并解压

编译安装搭建php环境

创建扩展项目

进入源码目录

cd php/ext/

/ext_skel extname=my_ext

创建名字为my_ext的项目最终会生成my_extso

更改配置和程序

$ vi ext/my_ext/configm

根据你自己的选择将

dnl PHP_ARG_WITH(my_ext for my_ext support

dnl Make sure that the comment is aligned:

dnl [ withmy_ext Include my_ext support])

修改成

PHP_ARG_WITH(my_ext for my_ext support

Make sure that the comment is aligned:

[ withmy_ext Include my_ext support])

或者将

dnl PHP_ARG_ENABLE(my_ext whether to enable my_ext support

dnl Make sure that the comment is aligned:

dnl [ enablemy_ext Enable my_ext support])

修改成

PHP_ARG_ENABLE(my_ext whether to enable my_ext support

Make sure that the comment is aligned:

[ enablemy_ext Enable my_ext support])

$ vi ext/my_ext/php_my_exth

PHP_FUNCTION(confirm_my_ext_compiled); /* For testing remove later */

更改为

PHP_FUNCTION(say_hello);

$ vi ext/my_ext/my_extc

zend_function_entry phpcpp_functions[] = {

PHP_FE(confirm_my_ext_compiled NULL) /* For testing remove later */

{NULL NULL NULL} /* Must be the last line in phpcpp_functions[] */

};

更改为

zend_function_entry phpcpp_functions[] = {

PHP_FE(say_hello NULL)

{NULL NULL NULL} /* Must be the last line in phpcpp_functions[] */

};

在最后添加

PHP_FUNCTION(say_hello)

{

zend_printf(hello world\n);

}

编译

$ cd my_ext

$ /usr/local/php/bin/phpize

ps: 如果出现Cannot find autoconf……的错误信息则需要安装 autoconf (安装过程略)

$ /configure withphpconfig=/usr/local/php/bin/phpconfig

$ make

这时会编译出 my_ext/modules/my_extso

配置phpini

将my_extso放入/usr/local/php/ext/目录

$ vi phpini

修改添加如下

extension_dir = /usr/local/php/ext/

extension=my_extso

测试

$ vi testphp

<?php

say_hello();

?>

$ /usr/local/php/bin/php testphp

hello world

则大功告成

               

上一篇:PHP面向对象编程快速入门

下一篇:php:修改NetBeans默认字体