Possible duplicate:
In PHP, can you instantiate an object and call a method on the same line?
Is it possible?
This usually requires two lines:
$instance = new MyClass();
$variable = $instance->method();
There is something similar in PHP:
$variable = new MyClass()->method();
Of course, the first code is better for readability and clean code, etc., but I was just curious if you could compress it. Maybe this can be useful if the method returns another instance, for example:
$instance = new MyClass()->methodThatReturnsInstance();
Is this possible in PHP?
source
share