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 git.master, git.master_jit, rfc.property-hooks
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.

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