3v4l.org

run code in 300+ PHP versions simultaneously
$value = null; $executed = false; return function () use ($producer, & $value, & $executed) { if (!$executed) { $value = $producer(); $executed = true; } return $value; }; <?php class Connection {} class Context { public function get_instance(){ static $i; $i ?: $i = new Connection(); return $i; } } class A extends Context {} class B extends A {} class Context2 { public function __constructor() { $this->conn = new Connection(); // Should be lazily created but imagine we have once(). } public function get_instance(){ return $this->conn; } } class A2 extends Context2 {} class B2 extends A2 {} $a = new A(); $b = new B(); $a2 = new A2(); $b2 = new B2(); var_dump($a->get_instance()); var_dump($b->get_instance()); var_dump($a2->get_instance()); var_dump($b2->get_instance());
Output for git.master, git.master_jit, rfc.property-hooks
$value = null; $executed = false; return function () use ($producer, & $value, & $executed) { if (!$executed) { $value = $producer(); $executed = true; } return $value; }; object(Connection)#5 (0) { } object(Connection)#5 (0) { } Warning: Undefined property: A2::$conn in /in/OYbWc on line 32 NULL Warning: Undefined property: B2::$conn in /in/OYbWc on line 32 NULL

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
65.06 ms | 402 KiB | 8 Q