php的函数的形参refcount为何要加2?
PHP中文网
PHP中文网 2017-04-10 14:27:53
0
3
419
<?php 
$b = 1;
a($b);
function a($a){
   xdebug_debug_zval('a');//refcount=3 why not 2??
}
PHP中文网
PHP中文网

认证高级PHP讲师

全員に返信(3)
巴扎黑

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

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

いいねを押す +0
PHPzhong

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.

いいねを押す +0
Peter_Zhu

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

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート