3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { function __construct (&$arr) { $this->arr = &$arr; } function createInstance () { $reflectionClass = new ReflectionClass("Bar"); return $reflectionClass->newInstanceArgs(array($this, $this->arr)); } function mod($key, $val) { $this->arr[$key] = $val; } } class Bar { function __construct (&$foo, &$arr) { $this->foo = &$foo; $this->arr = &$arr; } function mod($key, $val) { $this->arr[$key] = $val; } } $arr = array(); $foo = new Foo($arr); $arr["x"] = 1; $foo->mod("y", 2); $bar = $foo->createInstance(); $bar->mod("z", 3); var_dump($arr,$foo,$bar);

preferences:
47.69 ms | 402 KiB | 5 Q