When testing php about reference counting, I saw that a variable has a zval container. It contains two attributes, one is refcount and the other is_ref
$array= array( 'meaning' => 'life', 'number' => 42 );
xdebug_debug_zval( 'array' );
When testing the reference count of the array, the expected results are returned
array (refcount=1, is_ref=0),
array (size=2)
'meaning' => (refcount=1, is_ref=0),string ' life' (length=4)
'number' => (refcount=1, is_ref=0),int 42
But when testing objects. Have a question?
class A{
private $a1;
public $a2;
}
$class1 = new A();
xdebug_debug_zval('class1');
class1:
(refcount=1, is_ref=0),
object(A)[1]
private 'a1' => (refcount=2, is_ref =0),null
public 'a2' => (refcount=2, is_ref=0),null
Don’t quite understand why? Will this be 2? Instead of 1. Instead of like a php array. The expected refcount appears to be 1
And in the php official manual. An explanation of conforming types regarding reference counting.
php reference counting
Things are a little more complicated when it comes to composite types like array and object. Unlike values of scalar type, variables of array and object types store their members or properties in their own symbol tables. .
给我你的怀抱2017-06-27 09:20:05
PHP 5.6.19 显示如下
class X{
private $a;
public $b;
}
$x = new X();
xdebug_debug_zval("x");
(refcount=1, is_ref=0),
object(X)[3]
private 'a' => (refcount=1, is_ref=0),null
public 'b' => (refcount=1, is_ref=0),null
PHP Version:7.0.4
相同代码测试结果
x:
(refcount=1, is_ref=0)
object(X)[3]
private 'a' => (refcount=0, is_ref=0)null
public 'b' => (refcount=0, is_ref=0)null
PHP7中简单值直接使用zval容器存储,不再使用指向zval的指针,
也就是说PHP7操作变量是直接操作zval结构体,简单值不再参与垃圾回收
页不再参与引用计数,简单值得赋值直接拷贝,而不再计算引用计数
echo "<hr>PHP7简单值";
$m = 1;
$n = $m;
xdebug_debug_zval("m");
xdebug_debug_zval("n");
PHP7简单值m:
(refcount=0, is_ref=0)int 1
n:
(refcount=0, is_ref=0)int 1
如果简单值被引用了,则结果如下
echo "<hr>PHP7简单值被引用";
$m = 1;
$n = &$m;
xdebug_debug_zval("m");
xdebug_debug_zval("n");
m:
(refcount=2, is_ref=1)int 1
n:
(refcount=2, is_ref=1)int 1
可见当n引用m后,n、m指向同一个变量,引用计数为2,引用为true
unset($n);
m:
(refcount=1, is_ref=1)int 1
n:
(refcount=0, is_ref=0)*uninitialized*
可见unset掉n后,原变量的引用计数减为1,引用仍为true
``