Antarus
Antarus

Reputation: 1621

Passing multiple variables by reference in PHP 7.3 extension

I'm trying to pass multiple parameters (type ZVAL) by reference in a php extension function. But I'm not getting the changed value. I followed the suggestions from the following post. Passing a variable by reference into a PHP7 extension

But it worked for only one argument. Below is the code in which I'm trying to pass 2 ZVALs

PHP_FUNCTION(sample_byref_compiletime)
{
    zval *a,*b;     
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
        "z/z/", &a, &b ) == FAILURE)
    {
        php_printf("Error");
        RETURN_NULL();
    }
    ZVAL_DEREF(a);
    SEPARATE_ZVAL_NOREF(a);
    zval_dtor(a);   
    ZVAL_LONG(a, 40);

    ZVAL_DEREF(b);
    SEPARATE_ZVAL_NOREF(b);
    zval_dtor(b);   
    ZVAL_LONG(b, 41);
}

Upvotes: 0

Views: 554

Answers (1)

Roger Gee
Roger Gee

Reputation: 871

I think you may have misunderstood the answer to the post you linked in your question. Although, to your credit, it was a little confusing. The answer presented two possible solutions:

1.) Change z to z/ in the call to zend_parse_parameters()

or

2.) Add the calls to ZVAL_DEREF(a); and SEPARATE_ZVAL_NOREF(a); before reassigning the zval.

The second option essentially does what the / modifier would have done if provided in the call to zend_parse_parameters(). As the documentation indicates:

Advanced Type Specifiers

Spec       Description

...

/       SEPARATE_ZVAL_IF_NOT_REF on the parameter it follows

...

So you should either use just option #1 or just option #2. You applied both options at once, which means the dereference is happening twice.

Upvotes: 1

Related Questions