LINUX下PHP扩展模块的开发和测试(原创)

LINUX下PHP扩展模块的开发和测试(原创) 
作者:余超 Email:[email protected]
关于php的扩展模块开发,很多人都很害怕,我在新浪工作两年到现在的模块开发灵活运用,特发此文章供大家参考
首先确保你的开发环境配置正确,
我的如下:
[yuchao@yuchao-Latitude-E5410 branches_1]$env |grep PATH
CPLUS_INCLUDE_PATH=/usr/include:/usr/include/c++/4.5:/usr/share/ada/adainclude/rts-sjlj/adainclude:
LIBRARY_PATH=/usr/local/lib:/usr/share/ada/adainclude/rts-sjlj/adalib:
PATH=/home/yuchao/dev/hadoop/bin:/usr/local/jdk1.7.0/bin:/usr/local/mysql/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
C_INCLUDE_PATH=/usr/local/include:/usr/include/c++/4.5:/usr/share/ada/adainclude/rts-sjlj/adainclude:
[yuchao@yuchao-Latitude-E5410 branches_1]$gcc --version
gcc (Ubuntu/Linaro 4.4.4-14ubuntu5) 4.4.5
Copyright (C) 2010 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
等等一切工具
然后生成一个简单的php extension
我们需要两个目录:/home/yuchao/source/php-5.3.6/ext,到网上下载一个php源码包,解压,安装。
php的解压目录记为 source(如:/home/yuchao/source/php-5.3.6/ext) ,安装目录记为 /usr/bin(如 /usr/local/php)
在shell下输入(以后遇到有shell的地方我就用#开头,不另陈述)
# cd phpsrc/ext
# ./ext_skel --extname=yuchao
Creating directory yuchao
Creating basic files: config.m4 config.w32 .svnignore yuchao.c php_yuchao.h CREDITS EXPERIMENTAL tests/001.phpt yuchao.php [done].

To use your new extension, you will have to execute the following steps:
1.  $ cd ..
2.  $ vi ext/yuchao/config.m4
3.  $ ./buildconf
4.  $ ./configure --[with|enable]-yuchao
5.  $ make
6.  $ ./php -f ext/yuchao/yuchao.php
7.  $ vi ext/yuchao/yuchao.c
8.  $ make
Repeat steps 3-6 until you are satisfied with ext/yuchao/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

系统自动生成yuchao文件夹;接下来我们要修改几个文件:config.m4, yuchao.c,php_yuchao.h, 如下:
1) 修改config.m4
# cd yuchao
# vi config.m4
找到这几行
dnl PHP_ARG_ENABLE(yuchao, whether to enable yuchao support,
dnl Make sure that the comment is aligned:
dnl [  --enable-yuchao           Enable yuchao support])

去掉这几行前面的dnl,改为
PHP_ARG_ENABLE(yuchao, whether to enable yuchao support,
Make sure that the comment is aligned:
[  --enable-yuchao           Enable yuchao support])

这样以后编译php时,./configure后面加 --enable-yuchao 就可以加载你的php模块了!
(接下来的2)你可以做也可以不做,直接跳到第4步也可以运行。)
2) 修改yuchao.c,输出自己想要的东西
# vi yuchao.c
找到这段
PHP_FUNCTION(confirm_test_yuchao)
{
char *arg = NULL;
int arg_len, len;
char string[256];

if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {
return;
}
len = sprintf(string, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "yuchao", arg);
RETURN_STRINGL(string, len, 1);
}
改为:
PHP_FUNCTION(confirm_test_yuchao)
{
zend_printf("This is yuchao module !");
}
3)编译链接
# cd ../ext
# sudo cc -fpic -DCOMPILE_DL_TEST_MODULE=1 -I/usr/local/include -I. -I../main -I.. -I../TSRM -I../Zend -c -o yuchao/yuchao.o yuchao/yuchao.c
执行完之后会在 目录下生成一个test_module.o文件,接下来连接:
# sudo cc -shared -L/usr/local/lib -rdynamic -o yuchao/yuchao.so yuchao/yuchao.o
4)测试:
有两种途径可以测试你的扩展模块是否正确,一是在PATH目录下运行test.php, 二是在web browser上运行test.php(如果已经安装apache等web服务器的话)。
这里采用PATH测试,不需要任何web服务器。
拷贝test_module.so到PATH的相应目录下
(如果不知道是哪个目录,php.ini里设置extension_dir里会指出应该在什么路径。)
# mkdir -p /lib/php/extensions/ 
# cp yuchao/yuchao.so /lib/php/extensions/
在PATH目录下新建一个test.php文件,在里面写入
dl("yuchao.so");
//调用函数
yuchao();

?>

在PATH目录下运行
执行./php –q test.php,如果过程无误,将会显示:
This is yuchao module !

测试成功,接下来我们可以往扩展模块中添加自己的函数,实现自己的功能

下一篇将会介绍函数的功能实现,如果你等不急了,

就请自己看PHP ext目录下面的源代码吧。


发布了46 篇原创文章 · 获赞 5 · 访问量 15万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章