专注于互联网--专注于架构

最新标签
网站地图
文章索引
Rss订阅

首页 »PHP教程 » 编写自己的php扩展函数 »正文

编写自己的php扩展函数

来源: 发布时间:星期一, 2009年1月12日 浏览:37次 评论:0
  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文件中 3行注释就可以正常编译带这个自定义扩展模块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 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 o 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 '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])

   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_prf("hello worldn");
}


  保存文件退出

  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_prf系统在php中打印

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

  Vi my_module.c

  修改最后say_hello内容如下:

PHP_FUNCTION(say_hello)
{
    zval **yourname;
     (ZEND_NUM_ARGS != 1 || zend_get_parameters_ex(1, &yourname) FAILURE)
    {
        WRONG_PARAM_COUNT;
    }
    zend_prf("hello world, %sn", 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;
(ZEND_NUM_ARGS != 1 || zend_get_parameters_ex(1, &yourname) FAILURE)
{
WRONG_PARAM_COUNT;
}
zend_prf("hello world, %sn", Z_STRVAL_PP(yourname));


  代码解释如下:

  zval **yourname;

  个参数指针

  ZEND_NUM_ARGS

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

  zend_get_parameters_ex(1, &yourname)



  将刚刚指针指向传递过来参数如果不成功则报错

  Z_STRVAL_PP(yourname)

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



0

相关文章

读者评论

发表评论

  • 昵称:
  • 内容: