3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on j0BGR
Output for 7.0.0 - 7.2.0
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.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.
Output for 5.3.17 - 5.3.29
Parse error: syntax error, unexpected T_STRING in /in/jP1bu on line 3
Process exited with code 255.