Call an object like it's a function?

前端 未结 2 482
小蘑菇
小蘑菇 2021-01-21 11:23

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(         


        
相关标签:
2条回答
  • 2021-01-21 11:32

    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.

    0 讨论(0)
  • 2021-01-21 11:40

    Try with __construct() or __invoke().

    0 讨论(0)
提交回复
热议问题