Re: Compact can't resolve outer scoped variables using short closures
| From: | Claude Pache | Date: | Thu, 20 Oct 2022 07:15:01 +0000 |
| Subject: | Re: Compact can't resolve outer scoped variables using short closures | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118856@lists.php.net to get a copy of this message | ||
>
> How would you like it to work, if you pass a variable name variable to
> compact then?
> $x = 123;
> $name = 'x';
> (fn () => compact($name))();
>
Although it is difficult to make it work in general (of course), there is the specific case of names
given as literal strings, as in the example provided by the OP, that does not suffer from the
impossibility of static analysis.
> I agree with the idea of deprecating compact() & extract() and in
> long-term variable of variables as well.
However, in
compact('x', 'y'), the name of the variables are not
variable, they are constant.
—Claude