編寫自己的php擴展函數
php程序寫的時間長了,自然對他所提供的功能了如指掌,他所提供的一大堆功能,真是覺得很好用,但有時候會發現php也缺少一些功能,自己總是會產生為php添加一些自定義的功能的想法。久而久之,終于今天憋不住了,開始動手研究如何添加。;下載一個php的源代碼包,這里使用的是php 4.0.5版,解壓后會看到php的根目錄下會有README.EXT_SKEL這樣一個文件,打開詳細閱讀了一下,發現了一個非常好用的工具,這個工具可以幫你構建一個空的php擴展,然后你向里面添加相應的代碼就可以完成你自己的功能擴展了。下面我們就來介紹如何使用這個工具。;首先轉移你的目錄到php的目錄下的ext目錄,如果你只需要一個基本的擴展框架的話,執行下面的命令:./ext_skel --extname=module_namemodule_name是你自己可以選擇的擴展模塊的名字,例如我選擇的my_module。執行工具后會自動在ext目錄下建立你選擇的module_name名字的目錄,里面已經生成了相關的代碼,這些代碼中只需要調整config.m4文件中的三行注釋就可以正常的編譯帶這個自定義擴展模塊的php了。在php的根目錄執行下列操作就可以得到。./buildconf./configure --enable-module_namemake;下面我來演示建立my_module擴展框架的全過程,為了更有效果,我們來完成一個php的擴展功能,在php中調用這個功能可以在web頁面中顯示hello world這個經典單詞。在php目錄下的ext目錄中,執行下面的命令./ext_skel --extname=my_module得到反饋結果:Creating directory my_moduleCreating basic files: config.m4 Makefile.in .cvsignore my_module.c php_my_module.h tests/001.phpt my_module.php [done].;To use your new extension, you will have to execute the following steps:1. $ cd ..2. $ vi ext/my_module/config.m43. $ ./buildconf4. $ ./configure --[with|enable]-my_module5. $ make6. $ ./php -f ext/my_module/my_module.php7. $ vi ext/my_module/my_module.c8. $ make;Repeat steps 3-6 until you are satisfied with ext/my_module/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.;如果你能看懂上面的東西,那就照著去做。如果不是太明白的話,按照我下面的提示來做也可以。Cd my_module首先進入my_module目錄vi config.m4使用文本編輯器打開config.m4文件,文件內容大致如下:dnl $Id$dnl config.m4 for extension my_modulednl don't forget to call PHP_EXTENSION(my_module);dnl Comments in this file start with the string 'dnl'.dnl Remove where necessary. This file will not workdnl without editing.;dnl If your extension references something external, use with:;dnl PHP_ARG_WITH(my_module, for my_module support,dnl Make sure that the comment is aligned:dnl [ --with-my_module;Include my_module support]);dnl Otherwise use enable:;dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,dnl Make sure that the comment is aligned:dnl [ --enable-my_module; Enable my_module support]);if test "$PHP_MY_MODULE" != "no"then;dnl If you will not be testing anything external, like existence of;dnl headers, libraries or functions in them, just uncomment the;dnl following line and you are ready to go.;dnl Write more examples of tests here...;PHP_EXTENSION(my_module, $ext_shared)Fi;根據你自己的選擇將dnl PHP_ARG_WITH(my_module, for my_module support,dnl Make sure that the comment is aligned:dnl [ --with-my_module;Include my_module support])修改成PHP_ARG_WITH(my_module, for my_module support,Make sure that the comment is aligned:[ --with-my_module;Include my_module support])或者將dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,dnl Make sure that the comment is aligned:dnl [ --enable-my_module; Enable my_module support])修改成PHP_ARG_ENABLE(my_module, whether to enable my_module support,Make sure that the comment is aligned:[ --enable-my_module; Enable my_module support]);一般我會選擇后者,然后保存退出。如果你對vi文本編輯器的操作有困難的話,請參考相應的說明文章,這里就不再詳細描述了。Vi my_module.c將文件其中的下列代碼進行修改/* Every user visible function must have an entry in my_module_functions[].*/function_entry my_module_functions[] = {;;;;PHP_FE(say_hello,;;;NULL) /*ß添加著一行代碼*/;;;;PHP_FE(confirm_my_module_compiled,;;NULL) /* For testing, remove later. */;;;;{NULL, NULL, NULL};;/* Must be the last line in my_module_functions[] */};;在文件的最后添加下列代碼PHP_FUNCTION(say_hello){;;;;zend_printf("hello worldn");}保存文件退出;vi php_my_module.h在文件中PHP_FUNCTION(confirm_my_module_compiled);一行前面添加下面的代碼PHP_FUNCTION(say_hello);保存文件退出;退回到php的根目錄下,執行下面的命令./buildconf./configure --enable-my_modulemake;如果一切順利的話,我們現在已經將擴展模塊my_module編譯到php里面了。我們編寫下面的代碼進行測試<?;;;Say_hello();?>保存文件為say_hello.php在php的根目錄下運行./php –q say_hello.php正常情況下會顯示hello world表示我們的第一個擴展正常的運行了!;解釋一下上面做的操作,ext_skel生成一些框下文件,我們需要修改以下文件my_module.c 擴展模塊的主程序php_my_module.h擴展模塊的頭文件config.m4 配置文件;主程序中描述了php擴展模塊的聲明,模塊中含有多少個函數,各個函數的作用,在phpinfo函數中顯示什么內容,模塊初始化做些什么,結束做些什么都會在這個文件里進行描述。我們在上面只是添加了一個函數say_hello,并且描述了say_hello函數的具體內容,調用zend_printf系統函數在php中打印字符串。;在對應的頭文件中聲明了say_hello這個函數,從而完成了我們預期的功能。下面我們會編寫一個更復雜的擴展,創造一個帶參數的php擴展函數,根據給入的參數,顯示hello world, xxxx。Xxxx代表輸入的字符串內容,例如我的名字yorgo。;Vi my_module.c修改最后的say_hello函數內容如下:PHP_FUNCTION(say_hello){;;;;zval **yourname;;;;;;if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE);;;;{;;;;WRONG_PARAM_COUNT;;;;;};;;;;zend_printf("hello world, %sn", Z_STRVAL_PP(yourname));}存盤退出。退回php的根目錄,運行make修改say_hello.php為<?;;;Say_hello(“yorgo);?>保存退出后運行./php –q say_hello.php得出結果hello world, yorgo表示我們這次的修改也成功了,可以改變say_hello中的參數,看看動態的效果。這里主要解釋上面修改的函數內容,由于say_hello函數需要有參數引入,所以在my_module.c中的say_hello函數主要在進行參數的處理,將php中引用say_hello時所填寫的參數內容正確的傳遞到my_module.c中的say_hello處理函數中。為此,程序中添加了這么幾行。zval **yourname;if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &yourname) == FAILURE){WRONG_PARAM_COUNT;}zend_printf("hello world, %sn", Z_STRVAL_PP(yourname));;代碼解釋如下:zval **yourname;初始化一個參數的指針ZEND_NUM_ARGS()得到傳遞過來得參數數量,并且判斷如果不為1的時候表示有問題,報錯。zend_get_parameters_ex(1, &yourname)將剛剛初始化的指針指向傳遞過來的參數,如果不成功則報錯。Z_STRVAL_PP(yourname)處理指針指向的參數并獲得實際存儲的值。