首頁 > 後端開發 > php教程 > php的函数的形参refcount为何要加2?

php的函数的形参refcount为何要加2?

WBOY
發布: 2016-06-06 20:50:04
原創
1082 人瀏覽過

<code><?php $b = 1;
a($b);
function a($a){
   xdebug_debug_zval('a');//refcount=3 why not 2??
}
</code></code>
登入後複製
登入後複製

回复内容:

<code><?php $b = 1;
a($b);
function a($a){
   xdebug_debug_zval('a');//refcount=3 why not 2??
}
</code></code>
登入後複製
登入後複製

好吧,德问的lazyboy帮我查到答案了: 形参传值时的refcount

php的函数的形参refcount为何要加2?

例子内容来自http://julien-pauli.developpez.com/tutoriels/php/internals/variables/?page=page_6

A refcount of 2, here, is extremely non-obvious. Especially considering the above examples. So what's happening?

When a variable has a single reference (as did $var1 before it was used as an argument to debugzvaldump()), PHP's engine optimizes the manner in which it is passed to a function. Internally, PHP treats $var1 like a reference (in that the refcount is increased for the scope of this function), with the caveat that if the passed reference happens to be written to, a copy is made, but only at the moment of writing. This is known as "copy on write."

So, if debugzvaldump() happened to write to its sole parameter (and it doesn't), then a copy would be made. Until then, the parameter remains a reference, causing the refcount to be incremented to 2 for the scope of the function call.

xdebugdebugzval 是采用引用的方式传值的。

相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板