搜索
首页php教程php手册浅析PHP Extension开发基础篇(3)

浅析PHP Extension开发基础篇(3)

Jun 13, 2016 am 10:42 AM
parsephpzend一步参数基础开发解析通过

解析参数,这一步通过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

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
4 周前By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25:如何解锁Myrise中的所有内容
1 个月前By尊渡假赌尊渡假赌尊渡假赌

热工具

SecLists

SecLists

SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

功能强大的PHP集成开发环境

Atom编辑器mac版下载

Atom编辑器mac版下载

最流行的的开源编辑器

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )专业的PHP集成开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)