Defining ARG_INFO for object parameters

// ZEND_ARG_OBJ_INFO(pass_by_ref, name, classname, allow_null)
ZEND_BEGIN_ARG_INFO_EX(arginfo_test, 0, 0, 3)
    ZEND_ARG_INFO(0, firstArg)
    ZEND_ARG_OBJ_INFO(0, objStdClassNonNull, stdClass, 0)
    ZEND_ARG_OBJ_INFO(0, objStdClass, stdClass, 1)
    ZEND_ARG_OBJ_INFO(1, objStdClassByRef, stdClass, 1)
ZEND_END_ARG_INFO()

The code above creates the following function prototype:


function sample_with_types($firstArg, stdClass $objStdClassNonNull, stdClass $objStdClass = null, stdClass & $objStdClassByRef) { // ... }

It has the following effects:

sample_with_types();                          // ok
sample_with_types(1, null);                   // error: arg #2 should be stdClass
sample_with_types(1, new stdClass, null);     // ok
sample_with_types(1, new stdClass, 1);        // error: arg #3 should be stdClass
sample_with_types(1, new stdClass, null, 2);  // error: arg #4 must be reference

Defining ARG_INFO

The code below presents: ($environment, $response)

// ZEND_BEGIN_ARG_INFO_EX(name, _unused, return_reference, required_num_args)
ZEND_BEGIN_ARG_INFO_EX(arginfo_middleware_call, 0, 0, 2)
    ZEND_ARG_INFO(0, environment)
    ZEND_ARG_INFO(0, response)
ZEND_END_ARG_INFO()

The code below presents: (array & $environment, array $response)

ZEND_BEGIN_ARG_INFO_EX(arginfo_middleware_call, 0, 0, 2)
    // ZEND_ARG_ARRAY_INFO(pass_by_ref, name, allow_null)
    ZEND_ARG_ARRAY_INFO(1, environment, 0)
    ZEND_ARG_ARRAY_INFO(0, response, 0)
ZEND_END_ARG_INFO()

Parse Callable Parameter

The code below is from php-src (ext/standard/basic_functions.c)

    zval *retval_ptr = NULL;
    zend_fcall_info fci;
    zend_fcall_info_cache fci_cache;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f*", &fci, &fci_cache, &fci.params, &fci.param_count) == FAILURE) {
        return;
    }

    fci.retval_ptr_ptr = &retval_ptr;

    if (zend_call_function(&fci, &fci_cache TSRMLS_CC) == SUCCESS && fci.retval_ptr_ptr && *fci.retval_ptr_ptr) {
        COPY_PZVAL_TO_ZVAL(*return_value, *fci.retval_ptr_ptr);
    }

    if (fci.params) {
        efree(fci.params);
    }

PHP Extension Book Extension Cookbook!

Powered By