Home >Backend Development >PHP7 >Examples to explain the return value of PHP7 extension development

Examples to explain the return value of PHP7 extension development

藏色散人
藏色散人forward
2021-10-15 16:32:332829browse

Preface

This time, we will demonstrate how to accept incoming parameters and output return values ​​in PHP extensions.

<?php
    function default_value ($type, $value = null) {
        if ($type == "int") {
            return $value ?? 0;
        } else if ($type == "bool") {
            return $value ?? false;
        } else if ($type == "str") {
            return is_null($value) ? "" : $value;
        }
        return null;
    }
 
    var_dump(default_value("int"));
    var_dump(default_value("int", 1));
    var_dump(default_value("bool"));
    var_dump(default_value("bool", true));
    var_dump(default_value("str"));
    var_dump(default_value("str", "a"));
    var_dump(default_value("array"));
?>

We will implement the default_value method in the extension. [Recommended: "PHP7 Tutorial"]

Code

PHP extension source code to implement the default_value method

default_value method:

PHP_FUNCTION(default_value)
{
    zend_string     *type;    
    zval            *value = NULL;
 
#ifndef FAST_ZPP
    /* Get function parameters and do error-checking. */
    if (zend_parse_parameters(ZEND_NUM_ARGS(), "S|z", &type, &value) == FAILURE) {
        return;
    }    
#else
    ZEND_PARSE_PARAMETERS_START(1, 2)
        Z_PARAM_STR(type)
        Z_PARAM_OPTIONAL
        Z_PARAM_ZVAL_EX(value, 0, 1)
    ZEND_PARSE_PARAMETERS_END();
#endif
     
    if (ZSTR_LEN(type) == 3 && strncmp(ZSTR_VAL(type), "int", 3) == 0 && value == NULL) {
        RETURN_LONG(0);
    } else if (ZSTR_LEN(type) == 3 && strncmp(ZSTR_VAL(type), "int", 3) == 0 && value != NULL) {
        RETURN_ZVAL(value, 0, 1); 
    } else if (ZSTR_LEN(type) == 4 && strncmp(ZSTR_VAL(type), "bool", 4) == 0 && value == NULL) {
        RETURN_FALSE;
    } else if (ZSTR_LEN(type) == 4 && strncmp(ZSTR_VAL(type), "bool", 4) == 0 && value != NULL) {
        RETURN_ZVAL(value, 0, 1); 
    } else if (ZSTR_LEN(type) == 3 && strncmp(ZSTR_VAL(type), "str", 3) == 0 && value == NULL) {
        RETURN_EMPTY_STRING();
    } else if (ZSTR_LEN(type) == 3 && strncmp(ZSTR_VAL(type), "str", 3) == 0 && value != NULL) {
        RETURN_ZVAL(value, 0, 1); 
    } 
    RETURN_NULL();
}

Code description

Getting parameters

There are two methods of getting parameters in PHP7. zend_parse_parameters and FAST ZPP mode.

zend_parse_parameters

Before PHP7, the zend_parse_parameters function was used to obtain parameters. The function of this function is to convert the incoming parameters into the corresponding types in the PHP kernel, so that they can be used in PHP extensions.
Parameter description:
The first parameter, the number of parameters. Generally just use ZEND_NUM_ARGS(), no need to change.
The second parameter is the format string. The function of this format string is to specify the conversion relationship between the incoming parameters and the PHP core type.

The meaning of S|z in the code is:
S means that the parameter is a string. Convert the incoming parameters to zend_string type.
| indicates that the following parameters are optional. It can be passed on or not.
z indicates that the parameters are of multiple types. Convert the incoming parameters to zval type.

In addition, there are some specifiers that need attention:
! If a null variable in PHP language is received, it is directly converted into NULL in C language instead of encapsulating it into a zval of IS_NULL type.
/ If the passed variable shares a zval with other variables and is not a reference, forced separation is performed. The new zval's is_ref__gc==0, and refcount__gc==1.

More formats The meaning of the string can be viewed on the official website. https://wiki.php.net/rfc/fast_zpp

FAST ZPP

Newly provided in PHP7. It is to improve the performance of parameter parsing. For frequently used methods, it is recommended to use the FAST ZPP method.
Usage:
Start with ZEND_PARSE_PARAMETERS_START(1, 2).
The first parameter indicates the number of parameters that must be passed, and the second parameter indicates the maximum number of parameters that can be passed in.
Ends with ZEND_PARSE_PARAMETERS_END();.
The middle is the parsing of the incoming parameters.
It is worth noting that the macro method of FAST ZPP generally corresponds to the specifier of zend_parse_parameters. For example:
Z_PARAM_OPTIONAL corresponds to |
Z_PARAM_STR corresponds to S
However, the Z_PARAM_ZVAL_EX method is special. It corresponds to two specifiers, namely ! and /. ! Corresponds to the second parameter of the macro method. / corresponds to the third parameter of the macro method. If you want to enable it, just set it to 1.

The corresponding macro method of FAST ZPP can be viewed on the official website https://wiki.php.net/rfc/fast_zpp#proposal

Return value

The return value of the method is Use the macro method starting with RETURN_ to return. Commonly used macro methods are:
RETURN_NULL() Returns null
RETURN_LONG(l) Returns integer type
RETURN_DOUBLE(d) Returns floating point type
RETURN_STR(s) Returns a string. The parameter is a zend_string * pointer
RETURN_STRING(s) Returns a string. The parameter is a char * pointer
RETURN_STRINGL(s, l) returns a string. The second parameter is the string length.
RETURN_EMPTY_STRING() Returns an empty string.
RETURN_ARR(r) Returns an array. Parameters are zend_array* pointers.
RETURN_OBJ(r) Returns an object. Parameters are zend_object* pointers.
RETURN_ZVAL(zv, copy, dtor) returns any type. Parameters are zval* pointers.
RETURN_FALSE Returns false
RETURN_TRUE Returns true

The above is the detailed content of Examples to explain the return value of PHP7 extension development. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete