3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Hello2 { public function sayHello() { echo 'Hello '; } } class World2 { public function sayWorld() { echo 'World'; } } class MyHelloWorld2 { public $hello; public $world; public function __construct() { $this->hello = new Hello2(); $this->world = new World2(); } public function sayExclamationMark() { echo '!'; } public function __call($name, $arguments) { if (method_exists($this->hello, $name)) { var_dump('$this->hello->'.$name.'()'); // $this->hello->sayHello(); // 这个是可行的 // return $this->hello->sayHello(); // 这个就报错了 //return $this->hello->$name.'()'; return $this->hello->$name(); } if (method_exists($this->world, $name)) { // return $this->world->$name(); // 或者这样写 return call_user_func([$this->word, $name]); } if (!method_exists($this->hello, $name) && !method_exists($this->world, $name)) { echo '不存在此方法'; } } } $obj = new MyHelloWorld2(); $obj->sayHello(); $obj->sayWorld(); $obj->sayExclamationMark(); // 输出: // Hello World!
Output for 8.0.10 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
string(24) "$this->hello->sayHello()" Hello Warning: Undefined property: MyHelloWorld2::$word in /in/ggqAu on line 54 Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, first array member is not a valid class name or object in /in/ggqAu:54 Stack trace: #0 /in/ggqAu(65): MyHelloWorld2->__call('sayWorld', Array) #1 {main} thrown in /in/ggqAu on line 54
Process exited with code 255.
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 string(24) "$this->hello->sayHello()" Hello Warning: Undefined property: MyHelloWorld2::$word in /in/ggqAu on line 54 Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, first array member is not a valid class name or object in /in/ggqAu:54 Stack trace: #0 /in/ggqAu(65): MyHelloWorld2->__call('sayWorld', Array) #1 {main} thrown in /in/ggqAu on line 54
Process exited with code 255.
Output for 8.0.0 - 8.0.9
string(24) "$this->hello->sayHello()" Hello Warning: Undefined property: MyHelloWorld2::$word in /in/ggqAu on line 54 Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($function) must be a valid callback, first array member is not a valid class name or object in /in/ggqAu:54 Stack trace: #0 /in/ggqAu(65): MyHelloWorld2->__call('sayWorld', Array) #1 {main} thrown in /in/ggqAu on line 54
Process exited with code 255.
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.31, 7.4.0 - 7.4.33
string(24) "$this->hello->sayHello()" Hello Notice: Undefined property: MyHelloWorld2::$word in /in/ggqAu on line 54 Warning: call_user_func() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in /in/ggqAu on line 54 !
Output for 7.3.32 - 7.3.33
string(24) "$this->hello->sayHello()" Hello Warning: call_user_func() expects parameter 1 to be a valid callback, first array member is not a valid class name or object in /in/ggqAu on line 54 !

preferences:
220.3 ms | 402 KiB | 291 Q