In PHP, can you instantiate an object and call a method on the same line?

前端 未结 9 1816
没有蜡笔的小新
没有蜡笔的小新 2020-11-27 14:01

What I would like to do is something like this:

$method_result = new Obj()->method();

Instead of having to do:

$obj = ne         


        
9条回答
  •  迷失自我
    2020-11-27 14:29

    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:

    Class member access on instantiation has been added, e.g. (new Foo)->bar().

提交回复
热议问题