Ralph
Ralph

Reputation: 897

PHP: Instantiate a class and get property

Is it possible it instantiate a new class and get a property without assigning it to a variable object.

example:

$fileid = 1;
$filepath = new fileclass($fileid)->path;

I know the above is incorrect but is the concept possible?

Upvotes: 2

Views: 66

Answers (1)

Mark Baker
Mark Baker

Reputation: 212412

It is in PHP >= 5.4.0

$filepath = (new fileclass($fileid))->path;

The brackets are important

Upvotes: 6

Related Questions