解析參數,這一步通過zend_parse_parameters函數實現,這個函數的作用是從函數使用者的輸入棧中讀取資料,然後轉換成相應的函數參數填入變數以供後面核心功能代碼使用。zend_parse_parameters的第一個參數是使用者傳入參數的個數,可以由宏“ZEND_NUM_ARGS() TSRMLS_CC”產生;第二個參數是一個字串,其中每個字母代表一個變數類型,我們只有一個字串型變數,所以第二個參數是“s”;最後各個參數需要一些必要的局部變數指標用於儲存資料,下表給出了不同變數類型的字母代表及其所需要的局部變數指標。
參數解析完成後就是核心功能代碼,我們這裡只是輸出一行字元,php_printf是Zend版本的printf。
最後的傳回值也是通過宏實現的。RETURN_TRUE宏是返回布爾值“true”。
使用宏ZEND_BEGIN_ARG_INFO和ZEND_END_ARG_INFO定義參數資訊
參數資訊是函數所必要部分,這裡不做深究,直接給出相應代碼:
ZEND_BEGIN_ARG_INFO(arginfo_say_hello_func, 0) ZEND_END_ARG_INFO() 如需瞭解具體資訊請閱讀相關宏定義。
使用宏PHP_FE將函數加入到say_hello_functions中
最後,我們需要將剛才定義的函數和參數資訊加入到say_hello_functions數組裡,代碼如下:
const zend_function_entry say_hello_functions[] = { PHP_FE(say_hello_func, arginfo_say_hello_func) {NULL, NULL, NULL} }; 這一步就是通過PHP_EF宏實現,注意這個數組最後一行必須是{NULL, NULL, NULL} ,請不要刪除。
下面是編寫完成後的say_hello.c全部代碼:
/* +---------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | Copyright (c) 1997-2010 The PHP Group | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: | +----------------------------------------------------------------------+ */ /* $Id: header 297205 2010-03-30 21:09:07Z johannes ___FCKpd___14nbsp;*/ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php.h" #include "php_ini.h" #include "ext/standard/info.h" #include "php_say_hello.h" /* If you declare any globals in php_say_hello.h uncomment this: ZEND_DECLARE_MODULE_GLOBALS(say_hello) */ /* True global resources - no need for thread safety here */ static int le_say_hello; /* {{{ PHP_FUNCTION */ PHP_FUNCTION(say_hello_func) { char *name; int name_len; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE) { return; } php_printf("Hello %s!", name); RETURN_TRUE; } ZEND_BEGIN_ARG_INFO(arginfo_say_hello_func, 0) ZEND_END_ARG_INFO() /* }}} */ /* {{{ say_hello_functions[] * * Every user visible function must have an entry in say_hello_functions[]. */ const zend_function_entry say_hello_functions[] = { PHP_FE(say_hello_func, arginfo_say_hello_func) {NULL, NULL, NULL} /* Must be the last line in say_hello_functions[] */ }; /* }}} */ /* {{{ say_hello_module_entry */ zend_module_entry say_hello_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "say_hello", say_hello_functions, NULL, NULL, NULL, NULL, PHP_MINFO(say_hello), #if ZEND_MODULE_API_NO >= 20010901 "0.1", /* Replace with version number for your extension */ #endif STANDARD_MODULE_PROPERTIES }; /* }}} */ #ifdef COMPILE_DL_SAY_HELLO ZEND_GET_MODULE(say_hello) #endif /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION(say_hello) &nb
http://www.bkjia.com/PHPjc/478779.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/478779.htmlTechArticle解析參數,這一步通過zend_parse_parameters函數實現,這個函數的作用是從函數使用者的輸入棧中讀取資料,然後轉換成相應的函數參數填入變數...