Home Backend Development PHP Tutorial Writing parameter type bindings for PHP extension functions

Writing parameter type bindings for PHP extension functions

Dec 07, 2016 pm 03:25 PM
php

Let’s take a look at how to implement type binding through it, but this feature can only be used in Zend Engine 2, which is PHP5. Let's review ZE2's argument info structure again. 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 lines of ZEND_ARG_*INFO() macro function, and finally ends with the ZEND_END_ARG_INFO() macro function. If we want to rewrite the count() function in PHP language, we can:
ZEND_FUNCTION(sample_count_array)
{
 zval *arr;
 if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a",&arr) == FAILURE)
{
                                                                      using   using using                                                                         RETURN_LONG(zend_hash_num_elements(Z_ARRVAL_P(arr)));
}
zend_parse_parameters() itself can guarantee that the parameter passed is an array. But if we receive parameters through the zend_get_parameter() function, we are not so lucky, and we need to perform type checking 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 have handed over the type proofreading work to Zend Engine. Don’t you feel a sense 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 object in the argument and restrict it to inherit from a certain class or implementation a certain interface and so on.
ZEND_BEGIN_ARG_INFO(php_sample_class_arginfo, 0)
ZEND_ARG_OBJ_INFO(1, obj, stdClass, 0)
ZEND_END_ARG_INFO()
It should be noted that the value of the first parameter at this time is the number 1, which means it is passed by reference. In fact, this parameter is almost useless for objects, because all objects in ZE2 are passed by reference by default when used as function parameters. But we must set this parameter to the number 1, unless you don't want your extension to be compatible with PHP4. In PHP4, objects are passed as a complete copy, not by reference.

For array and object parameters, don’t forget the last parameter that is allowed to be NULL. For more information, please refer to the description in the last section of Chapter 6.
The function of type binding through arg info is only valid for ZE2, which is PHP5+. If you want to implement the corresponding function 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 type of the parameters or use the convert_to_type() function to perform type conversion.

Statement of this Website
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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles