日期:2014-04-19  浏览次数:20648 次

作者: 飘在四方
本人还没测试过,有兴趣的可以测试下

Yorgo Sun 2002/01/22



php程序写的时间长了,自然对他所提供的功能了如指掌,他所提供的一大堆功能,真是觉得很好用,但有时候会发现php也缺少一些功能,自己总是会产生为php添加一些自定义的功能的想法。久而久之,终于今天憋不住了,开始动手研究如何添加。



下载一个php的源代码包,这里使用的是php 4.0.5版,解压后会看到php的根目录下会有README.EXT_SKEL这样一个文件,打开详细阅读了一下,发现了一个非常好用的工具,这个工具可以帮你构建一个空的php扩展,然后你向里面添加相应的代码就可以完成你自己的功能扩展了。下面我们就来介绍如何使用这个工具。



首先转移你的目录到php的目录下的ext目录,如果你只需要一个基本的扩展框架的话,执行下面的命令:

./ext_skel --extname=module_name

module_name是你自己可以选择的扩展模块的名字,例如我选择的my_module。执行工具后会自动在ext目录下建立你选择的module_name名字的目录,里面已经生成了相关的代码,这些代码中只需要调整config.m4文件中的三行注释就可以正常的编译带这个自定义扩展模块的php了。在php的根目录执行下列操作就可以得到。

./buildconf

./configure --enable-module_name

make



下面我来演示建立my_module扩展框架的全过程,为了更有效果,我们来完成一个php的扩展功能,在php中调用这个功能可以在web页面中显示hello world这个经典单词。

在php目录下的ext目录中,执行下面的命令

./ext_skel --extname=my_module

得到反馈结果:

Creating directory my_module

Creating basic files: config.m4 Makefile.in .cvsignore my_module.c php_my_module.h tests/001.phpt my_module.php [done].



To use your new extension, you will have to execute the following steps:

1. $ cd ..

2. $ vi ext/my_module/config.m4

3. $ ./buildconf

4. $ ./configure --[with|enable]-my_module

5. $ make

6. $ ./php -f ext/my_module/my_module.php

7. $ vi ext/my_module/my_module.c

8. $ make



Repeat steps 3-6 until you are satisfied with ext/my_module/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.



如果你能看懂上面的东西,那就照着去做。如果不是太明白的话,按照我下面的提示来做也可以。

Cd my_module

首先进入my_module目录

vi config.m4

使用文本编辑器打开config.m4文件,文件内容大致如下:

dnl $Id$

dnl config.m4 for extension my_module

dnl don't forget to call PHP_EXTENSION(my_module)



dnl Comments in this file start with the string 'dnl'.

dnl Remove where necessary. This file will not work

dnl without editing.



dnl If your extension references something external, use with:



dnl PHP_ARG_WITH(my_module, for my_module support,

dnl Make sure that the comment is aligned:

dnl [ --with-my_module Include my_module support])



dnl Otherwise use enable:



dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

dnl Make sure that the comment is aligned:

dnl [ --enable-my_module Enable my_module support])



if test "$PHP_MY_MODULE" != "no"; then

dnl If you will not be testing anything external, like existence of

dnl headers, libraries or functions in them, just uncomment the

dnl following line and you are ready to go.

dnl Write more examples of tests here...

PHP_EXTENSION(my_module, $ext_shared)

Fi



根据你自己的选择将

dnl PHP_ARG_WITH(my_module, for my_module support,

dnl Make sure that the comment is aligned:

dnl [ --with-my_module Include my_module support])

修改成

PHP_ARG_WITH(my_module, for my_module support,

Make sure that the comment is aligned:

[ --with-my_module Include my_module support])

或者将

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,

dnl Make sure that the comment is aligned:

dnl [ --enable-my_module Enable my_module support])

修改成

PHP_ARG_ENABLE(my_module, whether to enable my_module support,

Make sure that the comment is aligned:

[ --enable-my_module Enable my_module support])



一般我会选择后者,然后保存退出。如果你对vi文本编辑器的操作有困难的话,请参考相应的说明文章,这里就不再详细描述了。

Vi my_module.c

将文件其中的下列代码进行修改

/* Every user vis