Slide 49
Slide 49 text
Proprietary and Confidential
Refcounting & PHP References
$a = 1; // $a = zval#1(value=1, refcount=1, is_ref=0)
$b = $a; // $a = $b = zval#1(value=1, refcount=2, is_ref=0)
$c = $b // $a = $b = $c = zval#1(value=1, refcount=3, is_ref=0)
$d =& $c; // $a = $b = zval#1(value=1, refcount=2, is_ref=0)
// $c = $d = zval#2(value=1, refcount=2, is_ref=1)
// $d is a reference of $c, but *not* of $a and $b, so
// the zval needs to be copied here. Now we have the
// same zval once with is_ref=0 and once with is_ref=1.
$d++; // $a = $b = zval#1(value=1, refcount=2, is_ref=0)
// $c = $d = zval#2(value=2, refcount=2, is_ref=1)
// Because there are two separate zvals $d++ does
// not modify $a and $b (as expected).