3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Human { public function speak() { return "I can speak"; } public function walk() { return "I can walk"; } public function jump() { return "I can jump"; } } /* calling parent is working but lots of duplication */ /* class Man extends Human { // can speak and walk but overrides some of the human function public function walk() { return parent::walk() . " fast"; } public function speak() { return parent::speak() . " fast"; } } */ class Man { // can speak and walk but overrides some of the human function public function __construct(Human $human) { $this->_human = $human; $this->_override = ["walk", "speak"]; } private function _method($parentResult) { return static::class . ": ". $parentResult . " fast"; } public function __call($name, $arguments) { return in_array($name, $this->_override) ? $this->_method($this->_human->$name()) : $this->_human->$name(); // here we could check if method exists } } // woman is an example for static access to instance methods of parent --> could be also static at parent (just easier to test with the code here) class Woman { private static function _method($parentResult) { return static::class . ": ". $parentResult . " fast"; } public static function __callStatic($name, $arguments) { $_override = ["walk", "speak"]; $_human = new Human(); return in_array($name, $_override) ? self::_method($_human->$name()) : $_human->$name(); // here we could check if method exists } } $human = new Human(); echo $human->speak() . "\r\n"; echo $human->walk(); echo "\r\n"; $man = new Man(new Human()); echo $man->speak() . "\r\n"; echo $man->walk() . "\r\n"; echo $man->jump(); echo "\r\n"; $woman = new Woman(); echo $woman::speak() . "\r\n"; echo $woman::walk() . "\r\n"; echo $woman::jump();
Output for git.master, git.master_jit, rfc.property-hooks
I can speak I can walk Deprecated: Creation of dynamic property Man::$_human is deprecated in /in/BYYDl on line 35 Deprecated: Creation of dynamic property Man::$_override is deprecated in /in/BYYDl on line 36 Man: I can speak fast Man: I can walk fast I can jump Woman: I can speak fast Woman: I can walk fast I can jump

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:
61.14 ms | 402 KiB | 8 Q