3v4l.org

run code in 300+ PHP versions simultaneously
<?php class prototype { private $inherited; private $instance; public function __construct(prototype $prototype = null) { $this->inherited = $prototype; $this->instance = []; } public function __set($name, $value) { if ($value instanceof \Closure) { $value = $value->bindTo($this); } $this->instance[$name] = $value; } public function __get($name) { if (array_key_exists($name, $this->instance)) { return $this->instance[$name]; } else { return $this->inherited->$name; } } public function __call($name, $arguments) { if (!isset($this->instance[$name])) { if (!isset($this->inherited) || !($fromPrototype = $this->inherited->$name) instanceof \Closure) { return; // or better, blow up } $this->instance[$name] = $fromPrototype->bindTo($this); } if (!($this->instance[$name] instanceof \Closure)) { return; // or better, blow up } return call_user_func_array($this->instance[$name], $arguments); } } $foo = new prototype; $foo->foo = 'Foo!'; $foo->bar = function() { return $this->foo; }; $bar = new prototype($foo); $bar->foo = 'Bar!'; echo $foo->foo . "\n"; echo $bar->foo . "\n";
Output for git.master, git.master_jit, rfc.property-hooks
Foo! Bar!

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:
50.93 ms | 401 KiB | 8 Q