什么是php模块开发?简单php模块开发介绍

伊谢尔伦
伊谢尔伦 原创
2023-03-11 06:44:01 4135浏览

在一些必要的场景我们不得不自己开发出自己的本地PHP函数满足一些特定的需求,而新的函数必须存在于PHP模块中。下面将介绍最简单的PHP模块开发:构建自己的say_hello($arg)函数来输出hello world : $arg。

本文档介绍的PHP模块开发仅仅是动动手做做hello world的程度,关于为什么这么做暂时不会介绍太多,更加详细的介绍后续解剖。

下面通过简单的几个步骤可以完成模块hello world级别的模块:

生成模块基础结构

修改模块代码,添加say_hello 函数

修改编译配置文件

生成模块共享库

配置模块,使模块生效

测试模块

1、生成模块基础

进入php源代码目录下的ext目录。
执行./ext_skel ––extname=sayhello (我这里的“–”编码有问题请不要直接拷贝)
输出:

[root@myhost ext]# ./ext_skel ––extname=sayhello

Creating directory sayhello

Creating basic files: config.m4 config.w32 .cvsignore sayhello.c php_sayhello.h CREDITS EXPERIMENTAL tests/001.phpt sayhello.php [done].

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

1. $ cd ..

2. $ vi ext/sayhello/config.m4

3. $ ./buildconf

4. $ ./configure ––[with|enable]-sayhello

5. $ make

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

7. $ vi ext/sayhello/sayhello.c

8. $ make

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

看到显示输出表示模块基础结构已经生成,我们来看下生成的模块包含哪些文件:

-rw-r–r– 1 root root 2103 Apr 9 05:05 config.m4 //编译配置文件

-rw-r–r– 1 root root 310 Apr 9 05:05 config.w32 //w32编译配置文件

-rw-r–r– 1 root root 8 Apr 9 05:05 CREDITS //作者信息

-rw-r–r– 1 root root 0 Apr 9 05:05 EXPERIMENTAL //测试版信息标识

-rw-r–r– 1 root root 2755 Apr 9 05:05 php_sayhello.h //模块定义头文件

-rw-r–r– 1 root root 5294 Apr 9 05:05 sayhello.c //模块实现文件

-rw-r–r– 1 root root 508 Apr 9 05:05 sayhello.php //用来测试模块加载的php文件

drwxr-xr-x 2 root root 4096 Apr 9 05:05 tests //测试文件目录

这个时候模块的骨架已经出来了,我们下一步就可以将目标的say_hello()函数加入。

2、实现say_hello()函数

打开模块的php_sayhello.h 文件,增加为php say_hello()准备的c函数定义:
PHP_FUNCTION(say_hello); //php 源代码为模块开放定义了许多宏,习惯了使用还是蛮方便的
增加完以后我们再对PHP_FUNCTION(say_hello)在say_hello.c中增加具体的实现:

PHP_FUNCTION(say_hello){
char *arg = NULL;
int arg_len;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) ==FAILURE) {  //获取php代码的输入参数,方式与scanf差不多
return;
}
zend_printf("hello world : %s",arg);
RETURN_STRINGL(arg, arg_len, 1);
}

现在实现代码也写了,我们还需要将这个函数注册到php本地函数中,需要修改sayhello.c中的sayhello_functions:

zend_function_entry sayhello_functions[] = {
PHP_FE(confirm_sayhello_compiled,    NULL)        /*
 For testing, remove later. */
PHP_FE(say_hello,    NULL)        //好,现在say_hello函数也注册了
{NULL, NULL, NULL}  
  /* Must be the last line in sayhello_functions[] */
};

3、修改编译配置文件

打开config.m4,将以下内容前的注释符“dnl”去掉:

dnl PHP_ARG_ENABLE(sayhello, whether to enable sayhello support,

dnl Make sure that the comment is aligned:

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

dnl PHP_SUBST(SAYHELLO_SHARED_LIBADD)

4、编译模块、生成共享库

我这里采用的是动态库的模块生成方式,比静态编译进php速度快多了,方便调试(使用上并非用php的dl()函数加载动态库,后续可以看到)。

进入(cd) sayhello模块文件夹,执行php安装路径bin下的phpize:

[root@myhost sayhello]# /opt/php_server/php/bin/phpize

Configuring for:

PHP Api Version: 20041225

Zend Module Api No: 20060613

Zend Extension Api No: 220060519

此时为模块编译的configure文件已经生成。继续生成Makefile文件:

[root@myhost sayhello]# ./configure –with-php-config=/opt/php_server/php/bin/php-config

……没问题的话是没有错误的

现在可以编译了:

make

代码没有问题的话不会有错的。

编译完,进行模块安装:

[root@myhost sayhello]# make install
Installing shared extensions: /opt/php_server/php/lib/php/extensions/no-debug-non-zts-20060613/

显示模块已经安装至/php安装路径/extensions/no-debug-non-zts-20060613/ 路径下了。

5、配置模块,使其被加载

打开你的php.ini文件开始修改吧:

扩展路径设置:

修改extension_dir = “/php安装路径/lib/php/extensions/no-debug-non-zts-20060613″ //看看上述的模块安装路径就知道了

增加模块设置:

[sayhello]
extension=sayhello.so

ok 大功告成,重新启动你的php模块把。

6、测试模块

写以下php测试代码执行:

<?php
$a = say_hello("frank");
echo "<br>";
echo $a;
?>;

打开这个网页后显示:

hello world : frank
frank

成功运行,模块测试通过。

以上就是什么是php模块开发?简单php模块开发介绍的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。