Heim  >  Artikel  >  Backend-Entwicklung  >  使用C++开发PHP扩展

使用C++开发PHP扩展

WBOY
WBOYOriginal
2016-08-08 09:21:421397Durchsuche
转自:http://521-wf.com/archives/241.html这篇文章主要介绍了用C++开发PHP扩展时,和C语言的区别目前,PHP编程语言也是相当成熟,各种文档,各种问题,只要Google一下,总有你想要的答案。当然“如何开发PHP扩展”的文章也不少,但是很少有专门来介绍使用C++开发PHP扩展的介绍。C++编程语言功能的强大,促使好多公司后台程序选择使用它,因此碰到的大多数情况是需要我们用C++来扩展 PHP。PHP源码中的扩展骨架工具,默认生成的是支持 C 语言,如果要使用C++开发,有些参数需要另行配置。下面将用一个简单的示例来说明。准备工作:1. 下载PHP源码 http://www.php.net/downloads,这里下载到的是 php-5.3.24.tar.gz
2. 解压后,源码放到 /root/php-5.3.24/
3. 安装目录:/usr/local/php-5.3.24/
4. 开始安装,配置 php.ini 的路径,方便以后进行个性化配置
5. 扩展名称:discuz
6. 扩展函数:discuz_say(),这个函数只返回一个“Hello world!” 字符串
7. 扩展可运行在 win32 系统,也运行在类unix系统,但是需要编译不同的文件,这里只介绍 GNU/Linux 下的操作。开始编写扩展:1. 创建要实现的函数列表文件 discuz.proto,内容如下:string discuz_say()2. 使用扩展骨架工具生成核心文件,命令如下:[root@localhost ~]# cd php-5.3.24/ext/ [root@localhost ext]# ./ext_skel --extname=discuz --proto=../../discuz.proto这时就在 ext 目录下出现了 discuz 文件夹,里面包含几个文件,如:config.m4 discuz.c  php_discuz.h 等等。3. 修改config.m4文件,内容如下:dnl $Id$ dnl config.m4 for extension discuz PHP_ARG_ENABLE(discuz, whether to enable discuz support, Make sure that the comment is aligned: [ --enable-discuz Enable discuz support]) if test "$PHP_DISCUZ" != "no"; then PHP_REQUIRE_CXX() dnl 通知Make使用g++ PHP_ADD_LIBRARY(stdc++, 1, EXTRA_LDFLAGS) dnl 加入C++标准库 PHP_NEW_EXTENSION(discuz, discuz.cpp, $ext_shared) fi这个文件中 dnl 是注释符,其之后的字串是解释上下文。4. 修改 discuz.c 文件重名为 discuz.cpp(这样命名看起来更专业)4.1 加入需要的C++ string 头文件,如下:#ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_discuz.h" #include /* 添加这行 */4.2 修改 discuz_say 函数,如下:/* {{{ proto string discuz_say() */ PHP_FUNCTION(discuz_say) { std::string str = "Hello world!"; RETURN_STRINGL(str.c_str(), str.length(), 1); }5. 编译扩展,把其 discuz.so 放到安装目录(可以参考 编译PHP扩展的两种方式)。开始测试:[root@localhost ~]# /usr/local/php-5.3.24/bin/php hi.php Hello world!到此,一个简单的扩展就完成了

以上就介绍了使用C++开发PHP扩展,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn