Why is calling property called in PHP 7?

Consider the following code:

interface Doll { /** * @return string */ function __invoke(); } class LargeDoll { private $inner; function __construct(Doll $inner) { $this->inner = $inner; } function __invoke() { return $this->inner() . ' world'; } } 

This will not work because $this->inner expected to be a method, not a property being called.

Then it occurred to me, just like work (new LargeDoll)(); will work, but what if the property were wrapped in paranthesis? So I tested it on 3v4l:

 return ($this->inner)() . ' world'; 

And found that it works for PHP 7, but not for previous versions.

However, I cannot find mention of this in the change lists .

Where can I find more information about this feature?

+7
php php-7
source share
1 answer

The ability to use IIFE (which ($this->inner)() effective) was added as part of Nikita Popov's Uniform Variable Syntax RFC , which was implemented in PHP7.

This is a result of improved processing of variable syntax in the parser. Given that one of the goals of PHP7 is to revise the parsing, I think that they have made real progress there.

+9
source share

All Articles