What I would like to do is something like this:
$method_result = new Obj()->method();
Instead of having to do:
$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?
The feature you have asked for is available from PHP 5.4. Here is the list of new features in PHP 5.4:
https://php-legacy-docs.zend.com/manual/php5/en/migration54.new-features
And the relevant part from the new features list:
Class member access on instantiation has been added, e.g.
(new Foo)->bar().
Starting with PHP 8.4, you can avoid using parentheses, i.e., new Foo()->bar()
. See RFC and the Release Announcement.