-
Notifications
You must be signed in to change notification settings - Fork 7.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: Anonymous Class Lexical Scope #1871
Conversation
|
||
if (zend_is_auto_global(var_name)) { | ||
zend_error_noreturn(E_COMPILE_ERROR, "Cannot use auto-global as lexical variable"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens with use($a, $a)
and similar?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solved for use($a, $a), but properties are a bit difficult to detect duplicates of, I think ...
💯 This is definitely one of the main reasons why writing anonymous classes is irritating now. new class use ($foo->bar as private $bar) would create private property `$bar. This would be also reusable for passing new class use ($this as private $outsideScope) |
Very cool! I don't know where to comment on the RFC so I'll comment here. If this is the syntax for variable capture in anonymous functions: function () use ($glow) {
print $glow;
} I would naively expect this to be the syntax for variable capture in anonymous classes: new class () use ($glow) {
public function printGlow() {
print $glow;
}
} But the correct code is actually: new class () use ($glow) {
public function printGlow() {
print $this->glow;
}
} This seems jarring to me. Might I suggest actually requiring the new class () use (private $glow) {
public function printGlow() {
print $this->glow;
}
} This also has a handy correspondence with the property+constructor initialization shorthand in Hack and TypeScript: class Foo {
public function __construct(private $glow) {
}
} |
zend_string_copy( | ||
zend_ast_get_str(use_var->child[1])); | ||
|
||
zend_compile_prop(&prop, use_var, BP_VAR_R); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For use(&$foo->bar)
shouldn't this compile BP_VAR_W
?
Another possible alternative: function foo($glow) {
new class () {
private $glow = $glow;
public function printGlow() {
print $this->glow;
}
}
} This makes it obvious how to use a different name, and possibly permits an arbitrary expression rather than just |
This patch is actually completely wrong, it's so obvious this morning ... The syntax can be changed after the fact, there is a problem with the implementation that must be fixed first. |
RFC