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 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
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
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 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
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
I can speak I can walk 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

preferences:
217.29 ms | 403 KiB | 291 Q