PHP7 extension development to participate in the return value

Source: Internet
Author: User
Tags php language rfc

Objective

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

<?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 ' approach in the extension.

Code

Base Code

This extension, we will add the ' Default_value ' method on the say extension. Say extension related code please read this blog post. PHP7 Extended development Hello Word has described in detail how to create an extension and provide a source download.

Implement Default_value method

Str_concat method of PHP extension source code:

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);} El Se 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 (v Alue, 0, 1); } return_null (); }

Code interpretation

Get parameters

Two methods of obtaining parameters are provided in PHP7. ' Zend_parse_parameters ' and fast zpp mode.

Zend_parse_parameters

Use the ' zend_parse_parameters ' function to get parameters before PHP7. The role of this function is to convert incoming parameters to the corresponding type in the PHP kernel, which is easy to use in PHP extensions.

Parameter description:

The first argument, the number of arguments. Generally use ' Zend_num_args ' (), no need to change.

The second argument, formatting the string. The purpose of this format string is to specify the transition relationship between the incoming parameter and the PHP kernel type.

The meaning of S|z in code is:

S indicates that the parameter is a string. To convert the incoming argument to the zend_string type.

| The parameters that are followed are optional. It can be transmitted or not transmitted.

Z indicates that parameters are multiple types. To convert the incoming argument to the Zval type.

In addition, there are some specifier that need to be noted:

! If a null variable is received in a PHP language, it is directly converted to NULL in C, rather than encapsulated as a is_null type of zval.

/If the passed variable is shared with another variable with a zval, and is not a reference, a forced separation is made, and the new Zval is_ref__gc==0, and Refcount__gc==1.

More formatting strings mean you can view the official web site. Https://wiki.php.net/rfc/fast_zpp

FAST ZPP

New ways of providing in PHP7. is to improve the performance of parameter parsing. The fast ZPP method is recommended for frequently used methods.

How to use:

Start with ' Zend_parse_parameters_start (1, 2) '.

The first parameter represents the required parameter format, and the second parameter represents the maximum number of arguments passed in.

End With ' Zend_parse_parameters_end ' ();

The middle is the parsing of the incoming arguments.

It is noteworthy that the general Fast ZPP macro method and zend_parse_parameters specifier is one by one corresponding. Such as:

Z_param_optional Correspondence |

Z_param_str corresponding S

However, the Z_PARAM_ZVAL_EX method is very special. It corresponds to two specifier, respectively is! and/.! The second parameter of the corresponding macro method. /The third parameter of the corresponding macro method. If you want to turn it on, just set it to 1.

FAST ZPP the appropriate macro method to view the official website https://wiki.php.net/rfc/fast_zpp#proposal

return value

The return value of the method is returned using a macro method that begins with ' Return_ '. The common macro methods are:

Return_null () returns NULL

Return_long (L) return integral type

Return_double (d) return floating-point type

RETURN_STR (s) returns a string. parameter is a zend_string * pointer

Return_string (s) returns a string. parameter is a char * pointer

Return_stringl (S, L) returns a string. The second argument is the length of the string.

Return_empty_string () returns an empty string.

Return_arr (R) returns an array. parameter is Zend_array * pointer.

Return_obj (R) returns an object. parameter is Zend_object * pointer.

Return_zval (Zv, copy, dtor) returns any type. parameter is Zval * pointer.

Return_false returns FALSE

Return_true returns TRUE

For more macro methods, see the code in zend/zend_api.h.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.