Nikita Popov wrote on 19/04/2016 15:18:
I'd like to suggest an alternative syntax:
$foo = 42;
return new class {
private $bar = $foo;
public function getBar() { return $this->bar; }
}
That is, allow properties inside the anonymous class to be initialized
based on values from the surrounding scope.
The problem with this is that it violates PHP's normal scoping rules,
because the variable is crossing scopes "uninvited". It makes it more
explicit where the variable is going *to*, but hides where it is coming
*from*.
Perhaps a Trait-like syntax might work?
$foo = 42;
return new class {
use $foo as private $bar;
public function getBar() { return $this->bar; }
}
This makes it clear both that $foo is being imported from somewhere, and
that it is being saved to a particular private member.
Regards,
--
Rowan Collins
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php