上一章已经写了个简单的C++扩展,这章开始写一个多文件的扩展包括面向对象的使用
1,在上一章成功的基础上,在ext_bankie/下建一个hello.cpp hello.h文件用来编写C++方法
然后编辑hello.h 内容如下:
int hello_add(int a,int b);
保存退出
2,编辑hello.cpp 内容如下:
7 extern "C"{
8 #include "php.h"
9 #include "php_ini.h"
10 #include "ext/standard/info.h"
11 }
12 #include "hello.h"
13
14
15 int hello_add(int a,int b){
16 return a+b;
17 }
~
保存退出,
3,我们编辑完C++有关文件之后,要去config.m4里,把要用到的CPP文件注册到PHP_NEW_EXTENSION里,,如下所示:
PHP_REQUIRE_CXX()
58 PHP_ARG_ENABLE(ext_bankie, whether to enable ext_bankie support,
59 Make sure that the comment is aligned:
60 [ --enable-ext_bankie Enable ext_bankie support])
61
62 PHP_ADD_LIBRARY(stdc++, ?.. EXTRA_LDFLAGS)
63 PHP_NEW_EXTENSION(ext_bankie, ext_bankie.cpp hello.cpp, $ext_shared)
64 fi
4,在ext_bankie.cpp里要使用hello_add的方法,所以编辑ext_bankie.cpp如下:
PHP_FUNCTION(helloadd){
201 long int a, b;
202 long int result;
203 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &a, &b) == FAILURE) {
204 return;
205 }
206 result= hello_add(a,b);
207 RETURN_LONG(result);
208 }
注
4,然后执行 configure命令,重新生成ext_bankie.so ,