3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private $bar = 'baz'; public function getBar() { return $this->bar; } } // http://3v4l.org/pc6lo#v540 class ClassicLazyFoo extends Foo { private $initialized = false; public function getBar() { $this->init(); return parent::getBar(); } private function init() { if ($this->initialized) { return; } \Closure::bind(function() { $this->bar = 'Loaded from DB!'; }, $this, get_parent_class($this))->__invoke(); } } // look ma: no method overrides! class LazyPropertyLazyFoo extends Foo { public function __construct() { \Closure::bind(function() { unset($this->bar); }, $this, get_parent_class($this))->__invoke(); } // note that other magic accessors also need to be created for full functional completeness public function __get($name) { \Closure::bind(function() { $this->bar = 'Loaded from DB!'; }, $this, get_parent_class($this))->__invoke(); return $this->$name; } } var_dump((new ClassicLazyFoo())->getBar()); var_dump((new LazyPropertyLazyFoo())->getBar());
Output for git.master, git.master_jit, rfc.property-hooks
string(15) "Loaded from DB!" Warning: Undefined property: LazyPropertyLazyFoo::$bar in /in/bCTP4 on line 52 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:
67.71 ms | 401 KiB | 8 Q