您好,欢迎访问一九零五行业门户网

PHP内核的学习-创办PHP扩展

php内核的学习--创建php扩展
开始看php内核也有一段时间了,现在开始边学边总结,今天就总结一下如何创建自己的php扩展。
我的环境如下:
系统:ubuntu 14.04
php版本:5.5.19
参考摘录:用c/c++扩展你的php
php取得成功的一个主要原因之一是它拥有大量的可用扩展。web开发者无论有何种需求,这种需求最有可能在php发行包里找到。php发行包包括支持各种数据库,图形文件格式,压缩,xml技术扩展在内的许多扩展。
扩展api的引入使php3取得了巨大的进展,扩展api机制使php开发社区很容易的开发出几十种扩展。现在,两个版本过去了,api仍然和php3时的非常相似。扩展主要的思想是:尽可能的从扩展编写者那里隐藏php的内部机制和脚本引擎本身,仅仅需要开发者熟悉api。
有两个理由需要自己编写php扩展。第一个理由是:php需要支持一项她还未支持的技术。这通常包括包裹一些现成的c函数库,以便提供php接口。例如,如果一个叫foobase的数据库已推出市场,你需要建立一个php扩展帮助你从php里调用foobase的c函数库。这个工作可能仅由一个人完成,然后被整个php社区共享(如果你愿意的话)。第二个不是很普遍的理由是:你需要从性能或功能的原因考虑来编写一些商业逻辑。
假设你正在开发一个网站,需要一个把字符串重复n次的函数。下面是用php写的例子:
function util_str_repeat($string, $n){ $result = ; for($i = 0; $i $n; $i++){ $result .= $string; } return $result;} util_str_repeat(one, 3);// returns oneoneone.util_str_repeat(one, 1);// returns one.
假设由于一些奇怪的原因,你需要时常调用这个函数,而且还要传给函数很长的字符串和大值n。这意味着在脚本里有相当巨大的字符串连接量和内存重新分配过程,以至显著地降低脚本执行速度。如果有一个函数能够更快地分配大量且足够的内存来存放结果字符串,然后把$string重复n次,就不需要在每次循环迭代中分配内存。
为扩展建立函数的第一步是写一个函数定义文件,该函数定义文件定义了扩展对外提供的函数原形。该例中,定义函数只有一行函数原形util_str_repeat() :
string util_str_repeat(string str, int n)
函数定义文件的一般格式是一个函数一行。你可以定义可选参数和使用大量的php类型,包括: bool, float, int, array等。
保存为util.def文件至php原代码目录树下(即与ext_skel文件放在同一目录下,我的目录是/usr/share/php5/)。
然后就是通过扩展骨架(skeleton)构造器运行函数定义文件的时机了。该构造器脚本就是ext_skel。假设你把函数定义保存在一个叫做util.def的文件里,而且你希望把扩展取名为util,运行下面的命令来建立扩展骨架:
sudo ./ext_skel --extname=util --proto=util.def
执行之后,我这里报了如下错误:
./ext_skel: 1: cd: can't cd to /usr/lib/php5/skeletoncreating directory utilawk: cannot open /create_stubs (no such file or directory)creating basic files: config.m4 config.w32 .svnignore util.c./ext_skel: 216: ./ext_skel: cannot open /skeleton.c: no such file php_util.h./ext_skel: 234: ./ext_skel: cannot open /php_skeleton.h: no such file credits./ext_skel: 238: ./ext_skel: cannot open /credits: no such file experimental./ext_skel: 242: ./ext_skel: cannot open /experimental: no such file tests/001.phpt./ext_skel: 247: ./ext_skel: cannot open /tests/001.phpt: no such file util.php./ext_skel: 251: ./ext_skel: cannot open /skeleton.php: no such filerm: cannot remove ‘function_entries’: no such file or directoryrm: cannot remove ‘function_declarations’: no such file or directoryrm: cannot remove ‘function_stubs’: no such file or directory [done].to use your new extension, you will have to execute the following steps:1. $ cd ..2. $ vi ext/util/config.m43. $ ./buildconf4. $ ./configure --[with|enable]-util5. $ make6. $ ./php -f ext/util/util.php7. $ vi ext/util/util.c8. $ makerepeat steps 3-6 until you are satisfied with ext/util/config.m4 andstep 6 confirms that your module is compiled into php. then, start writingcode and repeat the last two steps as often as necessary.
很明显是/usr/lib/php5/skeleton路径的错误,编辑ext_skel文件,将/usr/lib/php5/skeleton修改为/usr/share/php5/skeleton,然后移除掉生成的util文件夹,再次执行之前的命令,成功后提示如下:
creating directory utilcreating basic files: config.m4 config.w32 .svnignore util.c php_util.h credits experimental tests/001.phpt util.php [done].to use your new extension, you will have to execute the following steps:1. $ cd ..2. $ vi ext/util/config.m43. $ ./buildconf4. $ ./configure --[with|enable]-util5. $ make6. $ ./php -f ext/util/util.php7. $ vi ext/util/util.c8. $ makerepeat steps 3-6 until you are satisfied with ext/util/config.m4 andstep 6 confirms that your module is compiled into php. then, start writingcode and repeat the last two steps as often as necessary.
然后采用静态编译的方式编译扩展。为了使扩展能够被编译,需要修改扩展目录util/下的config.m4文件。扩展没有包裹任何外部的c库,你需要添加支持–enable-util配置开关到php编译系统里(–with-extension 开关用于那些需要用户指定相关c库路径的扩展)。找到如下内容:
dnl php_arg_enable(util, whether to enable util support,dnl make sure that the comment is aligned:dnl [ --enable-util enable util support])
将前面的dnl 去掉,修改为如下结果:
php_arg_enable(util, whether to enable util support,make sure that the comment is aligned:[ --enable-util enable util support])
然后修改util.c文件,找到如下代码:
php_function(util_str_repeat){ char *str = null; int argc = zend_num_args(); int str_len; long n; if (zend_parse_parameters(argc tsrmls_cc, sl, &str, &str_len, &n) == failure) return; php_error(e_warning, util_str_repeat: not yet implemented);}
将其修改为如下代码:
php_function(util_str_repeat){ char *str = null; int argc = zend_num_args(); int str_len; long n; char *result; /* points to resulting string */ char *ptr; /* points at the next location we want to copy to */ int result_length; /* length of resulting string */ if (zend_parse_parameters(argc tsrmls_cc, sl, &str, &str_len, &n) == failure) return; /* calculate length of result */ result_length = (str_len * n); /* allocate memory for result */ result = (char *) emalloc(result_length + 1); /* point at the beginning of the result */ ptr = result; while (n--) { /* copy str to the result */ memcpy(ptr, str, str_len); /* increment ptr to point at the next position we want to write to */ ptr += str_len; }
/* null terminate the result. always null-terminate your strings even if they are binary strings */ *ptr = '\0'; /* return result to the scripting engine without duplicating it*/ return_stringl(result, result_length, 0);}
里面的具体内容,就不在这里说了,之后会慢慢写到。
然后就是编译,安装。在util目录下,命令如下(命令可能都需要加sudo):
phpize./configuremakemake testmake install
然后配置生成的扩展文件,在php5.5版本中,进入到/etc/php5/mods-available目录下,创建util.ini文件,写入如下内容:
extension=util.so
然后enable util扩展
sudo php5enmod util
最后,重启php-fpm
sudo service php5-fpm restart
创建一个php文件,测试一下,测试文件如下:
phpfor ($i = 1; $i $i++) { print util_str_repeat(craryprimitiveman , $i); print \n;}?>
执行结果如下:
craryprimitiveman craryprimitiveman craryprimitiveman craryprimitiveman craryprimitiveman craryprimitiveman
这样我们就成功创建了一个包含简单的php函数的扩展。
盗图一张~~
今天就先到这里~~
1楼吉编辑很不错,感谢分享,有兴趣写书出书吗?re: 疯狂的原始人@吉编辑,水平不够啊。。。
其它类似信息

推荐信息