在PHP中,您可以实例化对象并在同一行上调用方法吗?
问题描述:
我想做的是这样的:
$method_result = new Obj()->method();
不必这样做:
$obj = new Obj();
$method_result = $obj->method();
在我的特定情况下,结果对我而言实际上并不重要.但是,有办法吗?
The result doesn't actually matter to me in my specific case. But, is there a way to do this?
答
PHP 5.4中提供了您所要求的功能.这是PHP 5.4的新功能列表:
The feature you have asked for is available from PHP 5.4. Here is the list of new features in PHP 5.4:
http://php.net/manual/en/migration54.new- features.php
以及新功能列表中的相关部分:
And the relevant part from the new features list:
已添加实例化的类成员访问权限,例如, (新Foo)-> bar().