Call an object like it's a function?

2019-02-25 17:15发布

Is there any way in PHP to call an object like it's a function?

class Funct {
  public function __callSelf() {
    echo 'Called!';
  }
}

$myFunc = new Funct();
$myFunc();

// Output

Called!

Something like this?

2条回答
地球回转人心会变
2楼-- · 2019-02-25 17:36

You can try with __invoke magic method.

The __invoke() method is called when a script tries to call an object as a function.

Note: This feature is available since PHP 5.3.0.

查看更多
贪生不怕死
3楼-- · 2019-02-25 17:36

Try with __construct() or __invoke().

查看更多
登录 后发表回答