background image

PHP 入门:用 语言编写 PHP 扩展

本篇文章是对用 C 语言编写 PHP 扩展进行了详细的分析介绍,需要的朋友参考下
1:预定义
在 home 目录,也可以其他任意目录,写一个文件,例如 caleng_module.def
内容是你希望定义的函数名以及参数:
int a(int x,int y)
string b(string str,int n)
 
2:到 php 源码目录的 ext 目录
#cd /usr/local/php-5.4.0/ext/
 
执行命令,生成对应扩展目录
#./ext_skel --extname=caleng_module --proto=/home/hm/caleng_module.def
 
3:修改 config.m4
去掉 dnl 的注释
PHP_ARG_ENABLE(caleng_module, whether to enable caleng_module support,
Make sure that the comment is aligned:
[ --enable-caleng_module Enable caleng_module support])
 
4:修改 caleng_module.c

 

复制代码 代码如下:
 

/* {{{ proto int a(int x, int y)
*/

PHP_FUNCTION(a)
{
int argc = ZEND_NUM_ARGS();
int x;
int y;
int z;

if

 (zend_parse_parameters(argc TSRMLS_CC, "ll", &x, &y) == FAILURE) 

 

return

;

z=x+y;
RETURN_LONG(z);