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.”
