Web lists-archives.com

Re: [PHP] 7.1 extension writing, output parameter remains unchanged




Hi Jan

2017-07-19 12:17 GMT+02:00 Jan Vávra <vavra@xxxxxx>:
> Hello,
>  I'm porting our extension from php5.6 to php7.1 x64 Windows, Visual Studio
> 2015.
> I'd like to write an extension function that has one input / output
> parameter type of long. But the parameter value changed in extension is not
> changed in php. What I'm doing wrong?
> The php code is:
>
> $a = 3;
> $r = hello_square($a);
> var_export($a); echo "\n"; //value of $a still remains 3, should be 9.
>
> The c code is:
>
> PHP_FUNCTION(hello_square)
> {
>   long a = 0;
>   zval *b;
>
>   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &b) == FAILURE)
> {
>     return;
>   }
>
>   a = Z_LVAL_P(b);
>   ZVAL_LONG(b, a*a);
>
>   RETURN_BOOL(1);
> }
>
> ----
>
> I also implemented hello_square this way
> PHP_FUNCTION(hello_square)
> {
>   zend_long b;
>
>   if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &b) == FAILURE)
> {
>     return;
>   }
>
>   b = b * b;
>   RETURN_BOOL(1);
> }
>
> And the value of $a remains unchanged.

What you should do is to make the parameter sent to hello_square() a
reference, this is done by the following (keep in mind this uses the
new and fast parameter parsing API, but it is still do-able with the
old zend_parse_parameters()):

https://gist.github.com/KalleZ/69f9ca1895bcac945e110b7777104203

Take a look at the README.PARAMETER_PARSING_API in the root of
php-src, this includes all possible values that
zend_parse_parameters() can accept




-- 
regards,

Kalle Sommer Nielsen
kalle@xxxxxxx

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php