编写自己的php扩展函数


Posted in PHP onOctober 09, 2006

编写自己的php扩展函数 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 visible function must have an entry in my_module_functions[].

*/

function_entry my_module_functions[] = {

        PHP_FE(say_hello,       NULL)  /* ß添加着一行代码 */

        PHP_FE(confirm_my_module_compiled,      NULL) /* For testing, remove later. */

        {NULL, NULL, NULL}      /* Must be the last line in my_module_functions[] */

};

  

在文件的最后添加下列代码

PHP_FUNCTION(say_hello)

{

        zend_printf("hello world\n");

}

保存文件退出

  

vi php_my_module.h

在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代码

PHP_FUNCTION(say_hello);

保存文件退出

  

退回到php的根目录下,执行下面的命令

./buildconf

./configure --enable-my_module

make

  

如果一切顺利的话,我们现在已经将扩展模块my_module编译到php里面了。我们编写下面的代码进行测试

<?

       Say_hello();

?>

保存文件为say_hello.php

在php的根目录下运行

./php ?q say_hello.php

正常情况下会显示

hello world

表示我们的第一个扩展正常的运行了!

  

解释一下上面做的操作,ext_skel生成一些框下文件,我们需要修改以下文件

my_module.c  扩展模块的主程序

php_my_module.h 扩展模块的头文件

config.m4  配置文件

  

主程序中描述了php扩展模块的声明,模块中含有多少个函数,各个函数的作用,在phpinfo函数中显示什么内容,模块初始化做些什么,结束做些什么都会在这个文件里进行描述。我们在上面只是添加了一个函数say_hello,并且描述了say_hello函数的具体内容,调用zend_printf系统函数在php中打印字符串。

  

在对应的头文件中声明了say_hello这个函数,从而完成了我们预期的功能。下面我们会编写一个更复杂的扩展,创造一个带参数的php扩展函数,根据给入的参数,显示hello world, xxxx。Xxxx代表输入的字符串内容,例如我的名字yorgo。

  

Vi my_module.c

修改最后的say_hello函数内容如下:

PHP_FUNCTION(say_hello)

{

        zval **yourname;

  

        if (ZEND_NUM_ARGS() != 1
zend_get_parameters_ex(1, &yourname) == FAILURE)

        {

                WRONG_PARAM_COUNT;

        }

  

        zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname));

}

存盘退出。

退回php的根目录,运行

make

修改say_hello.php为

<?

       Say_hello(“yorgo”);

?>

保存退出后运行

./php ?q say_hello.php

得出结果

hello world, yorgo

表示我们这次的修改也成功了,可以改变say_hello中的参数,看看动态的效果。

这里主要解释上面修改的函数内容,由于say_hello函数需要有参数引入,所以在my_module.c中的say_hello函数主要在进行参数的处理,将php中引用say_hello时所填写的参数内容正确的传递到my_module.c中的say_hello处理函数中。为此,程序中添加了这么几行。

zval **yourname;

if (ZEND_NUM_ARGS() != 1
zend_get_parameters_ex(1, &yourname) == FAILURE)

{

WRONG_PARAM_COUNT;

}

zend_printf("hello world, %s\n", Z_STRVAL_PP(yourname));

  

代码解释如下:

zval **yourname;

初始化一个参数的指针

ZEND_NUM_ARGS()

得到传递过来得参数数量,并且判断如果不为1的时候表示有问题,报错。

zend_get_parameters_ex(1, &yourname)

将刚刚初始化的指针指向传递过来的参数,如果不成功则报错。

Z_STRVAL_PP(yourname)

处理指针指向的参数并获得实际存储的值。

(待续)

PHP 相关文章推荐
php中判断一个字符串包含另一个字符串的方法
Mar 19 PHP
IIS php环境配置PHP5 MySQL5 ZendOptimizer phpmyadmin安装与配置
Nov 18 PHP
The specified CGI application misbehaved by not returning a complete set of HTTP headers
Mar 31 PHP
PHP 验证码的实现代码
Jul 17 PHP
php数组函数序列之array_key_exists() - 查找数组键名是否存在
Oct 29 PHP
PHP文件读写操作相关函数总结
Nov 18 PHP
php中Socket创建与监听实现方法
Jan 05 PHP
php结合正则批量抓取网页中邮箱地址
May 19 PHP
PHP中preg_match函数正则匹配的字符串长度问题
May 27 PHP
盘点PHP和ASP.NET的10大对比!
Dec 24 PHP
WordPress特定文章对搜索引擎隐藏或只允许搜索引擎查看
Dec 31 PHP
PHP PDOStatement::rowCount讲解
Feb 01 PHP
PHP输出控制功能在简繁体转换中的应用
Oct 09 #PHP
PHP 中执行系统外部命令
Oct 09 #PHP
PHP实现文件安全下载
Oct 09 #PHP
PHP - Html Transfer Code
Oct 09 #PHP
关于PHP中操作MySQL数据库的一些要注意的问题
Oct 09 #PHP
模拟xcopy的函数
Oct 09 #PHP
生成缩略图
Oct 09 #PHP
You might like
用PHP实现登陆验证码(类似条行码状)
2006/10/09 PHP
PHP 输出URL的快捷方式示例代码
2013/09/22 PHP
ThinkPHP实现递归无级分类――代码少
2015/07/29 PHP
php对文件夹进行相关操作(遍历、计算大小)
2015/11/04 PHP
php实现计算百度地图坐标之间距离的方法
2016/05/05 PHP
分析php://output和php://stdout的区别
2018/05/06 PHP
PHP实现数组和对象的相互转换操作示例
2019/03/20 PHP
Javascript的匿名函数小结
2009/12/31 Javascript
jquery动态加载select下拉框示例代码
2013/12/10 Javascript
jQuery获取页面元素绝对与相对位置的方法
2015/06/10 Javascript
在JavaScript中操作时间之setYear()方法的使用
2015/06/12 Javascript
js监听键盘事件的方法_原生和jquery的区别详解
2016/10/10 Javascript
jQuery基于ajax操作json数据简单示例
2017/01/05 Javascript
iview日期控件,双向绑定日期格式的方法
2018/03/15 Javascript
jQuery实现通过方向键控制div块上下左右移动的方法【测试可用】
2018/04/26 jQuery
JS实现字符串中去除指定子字符串方法分析
2018/05/17 Javascript
jQuery实现百度图片移入移出内容提示框上下左右移动的效果
2018/06/05 jQuery
bootstrap里bootstrap动态加载下拉框的实例讲解
2018/08/10 Javascript
vue3.0 CLI - 2.3 - 组件 home.vue 中学习指令和绑定
2018/09/14 Javascript
uni-app微信小程序登录授权的实现
2020/05/22 Javascript
antd form表单数据回显操作
2020/11/02 Javascript
[57:22]完美世界DOTA2联赛PWL S2 FTD vs PXG 第二场 11.27
2020/12/01 DOTA
使用requests库制作Python爬虫
2018/03/25 Python
Python函数装饰器实现方法详解
2018/12/22 Python
python3实现小球转动抽奖小游戏
2020/04/15 Python
django model object序列化实例
2020/03/13 Python
详解CSS的border边框属性及其在CSS3中的新特性
2016/05/10 HTML / CSS
HTML5 canvas画矩形时出现边框样式不一致的解决方法
2013/10/14 HTML / CSS
优衣库美国官网:UNIQLO美国
2018/04/14 全球购物
Nebula美国官网:便携式投影仪
2019/03/15 全球购物
某公司的.net工程师面试题笔试题
2013/11/22 面试题
家长给小学生的评语
2014/01/30 职场文书
培训主管的职业生涯规划
2014/03/06 职场文书
民间借贷协议书范本
2014/10/01 职场文书
小学生心理健康活动总结
2015/05/08 职场文书
golang语言指针操作
2022/04/14 Golang