技术开发 频道

浅谈PHP Extension的开发

  使用宏ZEND_BEGIN_ARG_INFO和ZEND_END_ARG_INFO定义参数信息

  参数信息是函数所必要部分,这里不做深究,直接给出相应代码:

1 ZEND_BEGIN_ARG_INFO(arginfo_say_hello_func, 0)  
2 ZEND_END_ARG_INFO()

  如需了解具体信息请阅读相关宏定义。

  使用宏PHP_FE将函数加入到say_hello_functions中

  最后,我们需要将刚才定义的函数和参数信息加入到say_hello_functions数组里,代码如下:

1 const zend_function_entry say_hello_functions[] = {  
2     PHP_FE(say_hello_func, arginfo_say_hello_func)  
3     {NULL, NULL, NULL}  
4 };

  这一步就是通过PHP_EF宏实现,注意这个数组最后一行必须是{NULL, NULL, NULL} ,请不要删除。

  下面是编写完成后的say_hello.c全部代码:

/*  
002   +----------------------------------------------------------------------+  
003   | PHP Version 5                                                        |  
004   +----------------------------------------------------------------------+  
005   | Copyright (c) 1997-2010 The PHP Group                                |  
006   +----------------------------------------------------------------------+  
007   | This source file is subject to version 3.01 of the PHP license,      |  
008   | that is bundled with this package in the file LICENSE, and is        |  
009   | available through the world-wide-web at the following url:           |  
010   | http://www.php.net/license/3_01.txt                                  |  
011   | If you did not receive a copy of the PHP license and are unable to   |  
012   | obtain it through the world-wide-web, please send a note to          |  
013   | license@php.net so we can mail you a copy immediately.               |  
014   +----------------------------------------------------------------------+  
015   | Author:                                                              |  
016   +----------------------------------------------------------------------+  
017 */
018    
019 /* $Id: header 297205 2010-03-30 21:09:07Z johannes $ */
020    
021 #ifdef HAVE_CONFIG_H  
022 #include "config.h"  
023 #endif  
024    
025 #include "php.h"  
026 #include "php_ini.h"  
027 #include "ext/standard/info.h"  
028 #include "php_say_hello.h"  
029    
030 /* If you declare any globals in php_say_hello.h uncomment this:  
031 ZEND_DECLARE_MODULE_GLOBALS(say_hello)  
032 */
033   
034 /* True global resources - no need for thread safety here */
035 static int le_say_hello;  
036    
037 /* {{{ PHP_FUNCTION  
038  */
039 PHP_FUNCTION(say_hello_func)  
040 {  
041     char *name;  
042     int name_len;  
043    
044     if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &name, &name_len) == FAILURE)  
045     {  
046         return;  
047     }  
048     php_printf("Hello %s!", name);  
049   
050     RETURN_TRUE;  
051 }  
052    
053 ZEND_BEGIN_ARG_INFO(arginfo_say_hello_func, 0)  
054 ZEND_END_ARG_INFO()  
055 /* }}} */
056    
057 /* {{{ say_hello_functions[]  
058  *  
059  * Every user visible function must have an entry in say_hello_functions[].  
060  */
061 const zend_function_entry say_hello_functions[] = {  
062     PHP_FE(say_hello_func, arginfo_say_hello_func)  
063     {NULL, NULL, NULL}  /* Must be the last line in say_hello_functions[] */
064 };  
065 /* }}} */
066    
067 /* {{{ say_hello_module_entry  
068  */
069 zend_module_entry say_hello_module_entry = {  
070 #if ZEND_MODULE_API_NO >= 20010901  
071     STANDARD_MODULE_HEADER,  
072 #endif  
073     "say_hello",  
074     say_hello_functions,  
075     NULL,  
076     NULL,  
077     NULL
078     NULL,  
079     PHP_MINFO(say_hello),  
080 #if ZEND_MODULE_API_NO >= 20010901  
081     "0.1", /* Replace with version number for your extension */
082 #endif  
083     STANDARD_MODULE_PROPERTIES  
084 };  
085 /* }}} */
086    
087 #ifdef COMPILE_DL_SAY_HELLO  
088 ZEND_GET_MODULE(say_hello)  
089 #endif  
090   
091 /* {{{ PHP_MINFO_FUNCTION  
092  */
093 PHP_MINFO_FUNCTION(say_hello)  
094 {  
095     php_info_print_table_start();  
096     php_info_print_table_header(2, "say_hello support", "enabled");  
097     php_info_print_table_row(2, "author", "Zhang Yang"); /* Replace with your name */
098     php_info_print_table_end();  
099    
100     /* Remove comments if you have entries in php.ini  
101     DISPLAY_INI_ENTRIES();  
102     */
103 }
104 /* }}} */
0
相关文章