Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

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

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?

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...