Compile PHP extension function parameter type binding

Source: Internet
Author: User
Let's take a look at how to implement type binding through it, but this feature can only be used in ZendEngine2, that is, PHP5. Let's review the structure of ZE2sargumentinfo. Every declaration of the arginfo structure is... let's take a look at how to implement type binding through it, but this feature can only be used in Zend Engine 2, that is, PHP5. Let's review the structure of ZE2's argument info. The declaration of each arg info structure starts with the ZEND_BEGIN_ARG_INFO () or ZEND_BEGIN_ARG_INFO_EX () macro function, followed by several rows of ZEND_ARG _ * INFO () macro functions, and finally uses ZEND_END_ARG_INFO () macro function ends. If you want to rewrite the count () function in PHP, you can:
ZEND_FUNCTION (sample_count_array)
{
Zval * arr;
If (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC, "a", & arr) = FAILURE)
{
RETURN_NULL ();
}
RETURN_LONG (zend_hash_num_elements (Z_ARRVAL_P (arr )));
}
Zend_parse_parameters () itself ensures that the passed parameter is an array. However, if we use the zend_get_parameter () function to receive parameters, we are not so lucky. we need to proofread the parameters by ourselves. If you want the kernel to automatically complete type verification, you need arg_info:
ZEND_BEGIN_ARG_INFO (php_sample_array_arginfo, 0)
ZEND_ARG_ARRAY_INFO (0, arr, 0)
ZEND_END_ARG_INFO ()

....
PHP_FE (sample_count_array, php_sample_array_arginfo)
....
In this way, we handed over the work of type verification to Zend Engine. Is there a feeling of relief! You 've also given your argument a name so that the generated error messages can be more meaningful to script writers attempting to use your API. we can also verify the objects in the parameters to restrict them from inheriting from a class or implementing an interface.
ZEND_BEGIN_ARG_INFO (php_sample_class_arginfo, 0)
ZEND_ARG_OBJ_INFO (1, obj, stdClass, 0)
ZEND_END_ARG_INFO ()
Note that the value of the first parameter is 1, which indicates that the parameter is passed as a reference. In fact, this parameter is almost useless for objects, because all objects in ZE2 are passed as function parameters by default. However, we must set this parameter to number 1 unless you do not want your extension to be compatible with PHP4. In PHP4, an object is a complete Copy passed, rather than a reference.

Do not forget the last allowed NULL parameter for array and object parameters. For more information, see the description in the last section of chapter 6th.
The type binding function through arg info is only valid for ZE2, that is, PHP5 +. If you want to implement the corresponding functions on PHP4, you need to use the zend_get_parameters () function to receive parameters, and then use the Z_TYPE_P () macro function to detect the parameter type or use convert_to_type () function for type conversion.

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.