Writing parameter type bindings for PHP extension functions
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.
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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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

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

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

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

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

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

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

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