3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait Traitor { public function cloneMutated($settings) { $object = clone $this; foreach ($settings as $name => $val) { $object->$name = $val; } return $object; } } class Q { use Traitor; private $foo; protected $bar; public function __construct($foo, $bar) { $this->foo = $foo; $this->bar = $bar; } } class Z extends Q { private $asd; } $a = new Z(1, 2); $b = $a->cloneMutated(["bar" => 3]); var_dump($b); $c = $a->cloneMutated(["asd" => 4]); var_dump($c);
Output for 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
object(Z)#2 (3) { ["foo":"Q":private]=> int(1) ["bar":protected]=> int(3) ["asd":"Z":private]=> NULL } Fatal error: Uncaught Error: Cannot access private property Z::$asd in /in/jP1bu:8 Stack trace: #0 /in/jP1bu(33): Q->cloneMutated(Array) #1 {main} thrown in /in/jP1bu on line 8
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30
object(Z)#2 (3) { ["asd":"Z":private]=> NULL ["foo":"Q":private]=> int(1) ["bar":protected]=> int(3) } Fatal error: Uncaught Error: Cannot access private property Z::$asd in /in/jP1bu:8 Stack trace: #0 /in/jP1bu(33): Q->cloneMutated(Array) #1 {main} thrown in /in/jP1bu on line 8
Process exited with code 255.
Output for 5.4.2 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
object(Z)#2 (3) { ["asd":"Z":private]=> NULL ["foo":"Q":private]=> int(1) ["bar":protected]=> int(3) } Fatal error: Cannot access private property Z::$asd in /in/jP1bu on line 8
Process exited with code 255.

preferences:
185.5 ms | 402 KiB | 227 Q