PHP 扩展函数返回的值为 NULL
我在开发 PHP 扩展时偶然发现了一个有趣的案例。在扩展代码中,我有:
PHP_FUNCTION(foo)
{
....
php_debug_zval_dump(return_value, 1);
}
在 PHP 代码中:
$v = foo();
debug_zval_dump($v);
运行上述代码时,我得到:
string(19) "Mouse configuration" refcount(1)
NULL refcount(2)
值未从扩展正确传递的原因是什么?
谢谢!
I've stumbled upon an interesting case while developing an extension for PHP. In the extension code I have:
PHP_FUNCTION(foo)
{
....
php_debug_zval_dump(return_value, 1);
}
In the PHP code:
$v = foo();
debug_zval_dump($v);
When running the above, I get:
string(19) "Mouse configuration" refcount(1)
NULL refcount(2)
What can be the reason that the value isn't passed properly from the extension?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不奇怪。
例如,如果您执行了
return_value = some_string_zval;
您将仅更改局部变量。php_debug_zval_dump
可以工作,但在函数之外没有任何效果。您必须主动复制 zval,例如使用:您可以从内部函数仅复制指针而不是复制数据返回的唯一情况是该函数通过引用返回。在这种情况下,您将获得一个
zval**
。It's not that strange.
For instance, if you did
return_value = some_string_zval;
you would be changing only the local variable.php_debug_zval_dump
would work, but it would have no effect outside the function. You have to actively copy the zval, e.g. with:The only case you could return from an internal function merely copying a pointer instead of copying data was if that function returned by reference. In that case, you're given a
zval**
.您得到 NULL 是因为 debug_zval_dump() 具有内置回显功能,并且您无法将回显设置为变量。所以你的 $v = foo() 实际上给你 $v = ""。空变量的引用计数为 2 的原因是由于固有的 PHP 优化。
在这里阅读相关内容: https://www.php.net /manual/en/function.debug-zval-dump.php
因此,要正确返回您的值,您可以:
以下是它的工作原理:
代码结果如下:
string(65) "string(19) "Mouse configuration" refcount(3) long(1) refcount(1) " refcount(2)
我不知道这段代码的用途是什么,但无论如何,祝你好运。 :)
You're getting a NULL because debug_zval_dump() has a built-in echo feature and you cannot set an echo to a variable. So your $v = foo() is actually giving you $v = "". The reason you're getting a refcount of 2 for an empty variable is because of inherent PHP optimization.
Read about that here: https://www.php.net/manual/en/function.debug-zval-dump.php
So to return your value properly you can:
Here's how it works:
The code will result with this:
string(65) "string(19) "Mouse configuration" refcount(3) long(1) refcount(1) " refcount(2)
I have no idea what this code is meant to do but Good Luck anyways. :)