php扩张

WBOY
Freigeben: 2016-06-13 11:02:25
Original
1053 Leute haben es durchsucht

php扩展

2008-02-27 00:29引用本文请注明出处:Just Do IT (http://www.toplee.com) 我从97年接触互联网的web开发,至今已经过去9年了,从最初的frontpage做html页面到学会ASP+access+IIS开始,就跟 web开发干上了,后来又依次使用了ASP+SQLServer+IIS、JSP+Oracle+Jrun(Resin/Tomcat)、PHP+ Syabse(MySQL)+Apache … 最后我定格到了 PHP+MySQL+Apache+Linux(BSD) 的架构上,也就是大家常说的LAMP架构,这说来有很多理由,网上也有很多人讨论各种架构和开发语言之间的优劣,我就不多说了,简单说一下我喜欢LAMP 的几个主要原因:1、全开放的免费平台;2、简单易上手、各种资源丰富;3、PHP、MySQL、Apache与Linux(BSD)系统底层以及彼此间无缝结合,非常高效;4、均使用最高效的语言C/C++开发,性能可靠;5、PHP语言和C的风格基本一致,还吸取了Java和C++的诸多架构优点;6、这是最关键的一点,那就是PHP可以非常方便的使用C/C++开发扩展模块,给了PHP无限的扩张性!基于以上原因,我非常喜欢基于PHP语言的架构,其中最关键的一点就是最后一点,以前在Yahoo和mop均推广使用这个平台,在C扩展php方面也有一些经验,在此和大家分享一下,希望可以抛砖引玉。用C语言编写PHP的扩展模块的方法有几种,根据最后的表现形式有两种,一种是直接编译进php,一种是编译为php的so扩展模块来被php调 用,另外根据编译的方式有两种,一种使用phpize工具(php编译后有的),一种使用ext_skel工具(php自带的),我们使用最多,也是最方 便的方式就是使用ext_skel工具来编写php的so扩展模块,这里也主要介绍这种方式。我们在php的源码目录里面可以看到有个ext目录(我这里说的php都是基于Linux平台的php来说的,不包括windows下的),在 ext目录下有个工具 ext_skel ,这个工具可以让我们简单的开发出php的扩展模块,它提供了一个通用的php扩展模块开发步骤和模板。下面我们以开发一个在php里面进行 utf8/gbk/gb2312三种编码转换的扩展模块为例子进行说明。在这个模块中,我们要最终提供以下几个函数接口:(1) string toplee_big52gbk(string s)将输入字符串从BIG5码转换成GBK(2) string toplee_gbk2big5(string s)将输入字符串从GBK转换成BIG5码(3) string toplee_normalize_name(string s)将输入字符串作以下处理:全角转半角,strim,大写转小写(4) string toplee_fan2jian(int code, string s)将输入的GBK繁体字符串转换成简体(5) string toplee_decode_utf(string s)将utf编码的字符串转换成UNICODE(6) string toplee_decode_utf_gb(string s)将utf编码的字符串转换成GB(7) string toplee_decode_utf_big5(string s)将utf编码的字符串转换成BIG5(8) string toplee_encode_utf_gb(string s)将输入的GBKf编码的字符串转换成utf编码首先,我们进入ext目录下,运行下面命令:#./ext_skel –extname=toplee这时,php会自动在ext目录下为我们生成一个目录toplee,里面包含下面几个文件.cvsignoreCREDITSEXPERIMENTALconfig.m4php_toplee.hteststoplee.ctoplee.php其中最有用的就是config.m4和toplee.c文件接下来我们修改config.m4文件#vi ./config.m4找到里面有类似这样几行dnl PHP_ARG_WITH(toplee, for toplee support,dnl Make sure that the comment is aligned:dnl [ --with-toplee Include toplee support])dnl Otherwise use enable:dnl PHP_ARG_ENABLE(toplee, whether to enable toplee support,dnl Make sure that the comment is aligned:dnl [ --enable-toplee Enable toplee support])上面的几行意思是说告诉php编译的使用使用那种方式加载我们的扩展模块toplee,我们使用–with-toplee的方式,于是我们修改为下面的样子PHP_ARG_WITH(toplee, for toplee support,Make sure that the comment is aligned:[ --with-toplee Include toplee support])dnl Otherwise use enable:dnl PHP_ARG_ENABLE(toplee, whether to enable toplee support,dnl Make sure that the comment is aligned:dnl [ --enable-toplee Enable toplee support])然后我们要做的关键事情就是编写toplee.c,这个是我们编写模块的主要文件,如果您什么都不修改,其实也完成了一个php扩展模块的编写,里面有类似下面的几行代码PHP_FUNCTION(confirm_toplee_compiled){   char *arg = NULL;   int arg_len, len;   char string[256];   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) {   return;   }   len = sprintf(string, "Congratulations! You have successfully modified ext/%.78s/config.m4. Module %.78s is now compiled into PHP.", "toplee", arg);   RETURN_STRINGL(string, len, 1);}如果我们在后面完成php的编译时把新的模块编译进去,那么我们就可以在php脚本中调用函数toplee(),它会输出一段字符串 “Congratulations! You have successfully modified ext/toplee/config.m4. Module toplee is now compiled into PHP.” 
Nach dem Login kopieren
Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!