PHP5.6版本扩展开发步骤
首先准备:
1. 系统环境:linux-ubuntu or centos or other
2. 安装:gcc make autoconf php
3. 下载:php源码 网址:php.net 选择php5.6版本的源码
解压PHP源码,进入源码目录。
从源码根目录,进入ext目录(该目录存入的为PHP扩展)
$ cd ext
Administrator@ZGC-20140530CYN /home/php-5.6.20/ext
$ ./ext_skel
./ext_skel --extname=module [--proto=file] [--stubs=file] [--xml[=file]]
[--skel=dir] [--full-xml] [--no-help]
--extname=module module is the name of your extension
--proto=file file contains prototypes of functions to create
--stubs=file generate only function stubs in file
--xml generate xml documentation to be added to phpdoc-svn
--skel=dir path to the skeleton directory
--full-xml generate xml documentation for a self-contained extension
(not yet implemented)
--no-help don't try to be nice and create comments in the code
and helper functions to test if the module compiled
我们只需要 ./ext_skel --extname=jisuan 就会产生一个jisuan扩展
cd jisuan
vim config.m4
找到第9行到第11行,把第10行,删除,结果如下:
PHP_ARG_WITH(jisuan, for jisuan support,
[ --with-jisuan Include jisuan support])
:wq 保存退出
phpize
./configure
make && make install
Installing shared extensions: /usr/lib/php/20131226/
Administrator@ZGC-20140530CYN /home/php-5.6.20/ext/jisuan
$ php -i |grep php.ini
Configuration File (php.ini) Path => /etc/php5
Loaded Configuration File => /etc/php5/php.ini (这就是.ini文件)
vim /etc/php5/php.ini 在最后一行添加:extension=jisuan.so
修改jisuan.h和jisuan.c文件
jisuan.h:
//添加如下方法 解释PHP_FUNCTION(扩展名_方法名) PHP_FUNCTION是zend底层提供与PHP扩展交互的函数
PHP_FUNCTION(jisuan_jia);
jisuan.c:
//实现上面定义的方法
PHP_FUNCTION(jisuan_jia)
{
long num1;
long num2;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ll", &num1, &num2) == FAILURE) {
return;
}
char *strg;
int len = spprintf(&strg, 0, "%d", num1+num2);
//返回类型可以从zend_api.h文件查询
RETURN_STRINGL(strg, len, 0);
//RETURN_LONG(num1+num2);
}
//找到如下方法
const zend_function_entry jisuan_functions[] = {
PHP_FE(confirm_jisuan_compiled,NULL)/* For testing, remove later. */
PHP_FE(jisuan_jia,NULL)//添加自己定义的方法
//PHP_FE(jisuan_jian,NULL)
//PHP_FE(jisuan_cheng,NULL)
//PHP_FE(jisuan_chu,NULL)
PHP_FE_END/* Must be the last line in jisuan_functions[] */
};
命令行操作:
make && make install
然后在新建test.php文件
var_dump(jisuan_jia(100,200));
?>
执行:php test.php 结果:string("300")
到此,PHP扩展开发已经结束,但是在APACHE里可能还没有
因为apache的php.ini是独立的,所以,在apache的php.ini将此扩展添加即可以(方法同:php的php.ini修改添加一样)