3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 5.6.0 - 5.6.30, 7.0.0 - 7.3.0rc3
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 hhvm-3.15.4
string(24) "$this->hello->sayHello()" Hello Notice: Undefined property: MyHelloWorld2::$word in /in/ggqAu on line 54 Warning: Invalid argument: function: not a valid callback array in /in/ggqAu on line 54 !