Re: RFC: Anonymous Class Lexical Scope
| From: | Rowan Collins | Date: | Tue, 19 Apr 2016 16:00:28 +0000 |
| Subject: | Re: RFC: Anonymous Class Lexical Scope | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-92473@lists.php.net to get a copy of this message | ||
Hi Nikita,
I was thinking some more about your suggestion:
Nikita Popov wrote on 19/04/2016 15:18:
$foo = 42;
return new class {
private $bar = $foo;
public function getBar() { return $this->bar; }
}
If we allowed this, would it make sense to also allow it in named classes? This is currently illegal [https://3v4l.org/Kp0ep]:
$foo = 42;
class X {
private $bar = $foo;
public function getBar() { return $this->bar; }
}
It probably doesn't make sense to
restrict this to specific expressions, so all of
return new class {
private $a = $var;
private $b = $obj->prop;
private $d = $obj->prop ?? 'default';
// ...
}
could be fine.
We don't allow importing expressions into closures, nor are any of those property initialisations valid in the definitions of named classes, so I think allowing them here would be odd.
Regards,
--
Rowan Collins
[IMSoP]