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 be2? 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. .
业精于勤,荒于嬉;行成于思,毁于随。