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

前端 未结 9 1814
没有蜡笔的小新
没有蜡笔的小新 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:23

    You can do it more universally by defining an identity function:

    function identity($x) {
        return $x;
    }
    
    identity(new Obj)->method();
    

    That way you don't need to define a function for each class.

提交回复
热议问题