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 discuzphp_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教程有兴趣的朋友有所帮助。
