3v4l.org

run code in 300+ PHP versions simultaneously
<?php class demo { public $n = 0; function __construct() { $this->n =& $this->n; } function magic() { if ($this->n) return; } function set($n) { $this->n = $n; } } // Outputs 1 = 1 as expected $a1 = new demo(); $b1 = clone $a1; $a1->magic(); $b1->set(1); echo "{$a1->n} = {$b1->n}\n"; // Outputs 0 = 1 (magic() method is called a little earlier) $a2 = new demo(); $a2->magic(); $b2 = clone $a2; $b2->set(1); echo "{$a2->n} = {$b2->n}\n";
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.21, 7.4.0 - 7.4.10, 8.0.0alpha1 - beta3
0 = 1 0 = 1