Re: RFC: Anonymous Class Lexical Scope
| From: | Dmitry Stogov | Date: | Tue, 19 Apr 2016 14:35:27 +0000 |
| Subject: | Re: RFC: Anonymous Class Lexical Scope | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-92465@lists.php.net to get a copy of this message | ||
On 04/19/2016 05:18 PM, Nikita Popov wrote:
On Tue, Apr 19, 2016 at 3:31 PM, Joe Watkins <pthreads@pthreads.org> wrote:This syntax is definitely better, less magic and more powerful.Morning Internals,Hey Joe, The syntax and semantics proposed in this RFC don't sit quite well with me. Especially the fact that a use($foo) on the class is then used as $this->foo in methods is non-intuitive to me, as it differs from how the same syntax behaves on closures. I'd like to suggest an alternative syntax: $foo = 42; return new class {Please review the following RFC:https://wiki.php.net/rfc/lexical-anonA look at the patch from those of you that do that would be good :)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. This is more explicit (clearly
shows that a property is being created), it allows explicit control over
the visibility and, depending on implementation, might be more flexible
with regards to the values it accepts. 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.
Is this for anonymous classes only?
Only for property default values or something else?
Also, instead of lexical scoping, we may use generic approach.
return new class<A=&$var,B=$obj->prop,C=$obj->prop ?? 'default'> {
private $a = A;
private $b = B;
private $d = C;
};
But good design of generics would take a lot of ...
Thanks. Dmitry.
Thanks, Nikita