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]) && isset($this->inherited) && ($fromProto = $this->inherited->$name) instanceof \Closure) { $target = $fromProto->bindTo($this); } else if (isset($this->instance[$name]) && $this->instance[$name] instanceof \Closure) { $target = $this->instance[$name]; } else { return; } return call_user_func_array($target, $arguments); } } $foo = new prototype; $foo->foo = 'Foo!'; $foo->bar = function() { return $this->foo; }; $bar = new prototype($foo); $bar->foo = 'Bar!'; echo $foo->bar() . "\n"; echo $bar->bar() . "\n"; $foo->bar = function() { return "Baz!"; }; echo $foo->bar() . "\n"; echo $bar->bar() . "\n";
Output for git.master, git.master_jit, rfc.property-hooks
Foo! Bar! Baz! Baz!

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