有没有办法编写一个可以像函数一样调用的类?我的意思是这样的:
class test{}
$test = new test;
$test();
我尝试使用__call
魔术方法,但只有在访问其中存在的方法时才能使用它。也许可以使用SPL类或接口来完成?
答案 0 :(得分:5)
class test{
public function __invoke()
{
return 'fooo';
}
}
$test = new test;
print $test();
但是你需要PHP 5.3。
另一种替代方案取决于您尝试执行的操作,可能使用closure。