您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

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

2025/11/6 8:28:05发布20次查看
在一些必要的场景我们不得不自己开发出自己的本地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模块开发介绍的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product