search
Homephp教程php手册浅析PHP Extension开发基础篇(3)

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

Jun 13, 2016 am 10:42 AM
parsephpzendstepparameterBasedevelopparsepass

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

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.