A Preliminary Study on the PHP extension

知者不言 ,言者不知。

有时,看起来简单的PHP并不够用。尽管不够用的情况对普通的程序员很少见,而专业的程序员很快就能将PHP带到它的边界(瓶颈),从性能或功能方面看。新的功能不是生来就能被实现,是因为程序语言固有的限制和复杂,当不得不附添加巨大的默认代码库到每个简单脚本上时,就会出现这些限制和麻烦,于是需要寻找另一种方式来克服PHP这些终极缺陷。

基于上述说法,是时候接触PHP的心脏并看看它的核心了,即让PHP运行的C代码。

编写ext/extsion.def文件

string extions_function(string str, int n )

运行配置文件

➜ ext git:(master) ✗ ./ext_skel --extname=myfunctions --proto=myfunctions.def
Creating directory myfunctions
awk: /home/zongbao/php-src/ext/skeleton/create_stubs:56: warning: escape sequence `\|' treated as plain `|'
Creating basic files: config.m4 config.w32 .gitignore myfunctions.c php_myfunctions.h CREDITS EXPERIMENTAL tests/001.phpt myfunctions.php [done].
To use your new extension, you will have to execute the following steps:
1. $ cd ..
2. $ vi ext/myfunctions/config.m4
3. $ ./buildconf
4. $ ./configure --[with|enable]-myfunctions
5. $ make
6. $ ./sapi/cli/php -f ext/myfunctions/myfunctions.php
7. $ vi ext/myfunctions/myfunctions.c
8. $ make
Repeat steps 3-6 until you are satisfied with ext/myfunctions/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.

编辑config.m4,取消下面两行的注释

PHP_ARG_ENABLE(myfunctions, whether to enable myfunctions support,
dnl Make sure that the comment is aligned:
[ --enable-myfunctions Enable myfunctions support])

configure

./configure --enable-myfunctions

make
make install

###Points

78 PHP_FUNCTION(self_concat)
79 {
80 char *str = NULL;
81 int argc = ZEND_NUM_ARGS();
82 size_t str_len;
83 zend_long n;
84
85
86 char *result; /*points to resulting string*/
87 char *ptr; /*points to the next location we want to copy*/
88 int result_length; /*length of resulting string*/
89
90
91 if (zend_parse_parameters(argc TSRMLS_CC, "sl", &str, &str_len, &n) == FAILURE)
92 return;
93
94 /*calculate length of result*/
95 result_length = (str_len * n);
96
97 result = (char *) emalloc(result_length + 1);
98 ptr = result;
99 while (n--) {
100 memcpy(ptr, str, str_len);
101 ptr += str_len;
102 }
103
104 *ptr = '\0';
105 /* Return result to the scripting engine without duplicating it*/
106 RETURN_STRINGL(result, result_length);
107
108 //php_error(E_WARNING, "self_concat: not yet implemented");
109 }

###Test

1 <?php
2
3
4 //phpinfo();
5 echo self_concat('feng', 9);
6
7

###Output

➜ myfunctions git:(master) ✗ php phpinfo.php
fengfengfengfengfengfengfengfengfeng%

####ctags

把光标移动到某个元素上,CTRL+],就会跳转到对应的定义啦。CTRL+o可以回退到原来的地方。
还有一个操作经常被人忽略,就是,如果当前光标下是个局部变量,在VIM里输入gd的话,就会跳到这个局部变量的定义处

$ ctags -R
$ gd
$ CTRL+]
$ CTRL+o

每次都要输入这个才能使tags生效,我很懒,你也可以将这句话放到~/.vimrc中去:

set tags+=/home/zongbao/php-src/tags

####官方文档:

http://php.net/manual/zh/internals2.ze1.zendapi.php

Zend_API、PHP函数定义查找网站:
http://lxr.php.net/

####几篇C编程文章:

https://ring0.me/2014/11/c-compiler/
http://mooc.guokr.com/note/13202/
http://www.ruanyifeng.com/blog/2014/11/compiler.html
http://www.laruence.com/2009/04/28/719.html
https://code.google.com/p/phpqqgroup/source/browse/trunk/Php/PHP.5.Power.Programming.chm

script>