【发布时间】:2013-07-02 19:29:47
【问题描述】:
我想立即在新构造的对象上调用魔术方法 (__call())。示例:
class Foo {
public function __call($method,$args) {
echo "You were looking for the method $method.\n";
}
}
理想(但出现解析错误):
$foo = new Foo()->bar(); // Fails :(
工作:
$foo = new Foo();
$foo = $foo->bar();
这可能吗?我知道 PHP 5.4 带来了直接的 1 行对象方法调用 (http://docs.php.net/manual/en/migration54.new-features.php),所以我不确定为什么这不起作用。
【问题讨论】:
-
您希望
$foo的值是Foo类的新实例还是bar方法的返回结果? -
方法
bar的结果。 @complex857 有一个有效的答案。
标签: php oop magic-function