解析参数,这一步通过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函数实现,这个函数的作用是从函数用户的输入栈中读取数据,然后转换成相应的函数参数填入变量...