Re: Compact can't resolve outer scoped variables using short closures
| From: | David Rodrigues | Date: | Wed, 19 Oct 2022 17:17:53 +0000 |
| Subject: | Re: Compact can't resolve outer scoped variables using short closures | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118853@lists.php.net to get a copy of this message | ||
> I'd rather hope for
compact() to finally be deprecated and
> targeted for
removal 😛
I think compact() is a good function for transferring variables from one
point to another, but I would think about making improvements as it is
confusing (uses the variable name, rather than the variable itself).
Regarding the bug, if we use an array it should work perfectly:
$x = 123;
(fn() => [ 'x' => $x ])();
https://3v4l.org/ov7TM
Would it be possible to automatically convert compact() to an array at
runtime? So I imagine that any necessary optimization can take place
directly over the resulting array, rather than the compact itself.
Atenciosamente,
David Rodrigues
Em qua., 19 de out. de 2022 às 14:09, Marco Pivetta <ocramius@gmail.com>
escreveu:
> On Wed, 19 Oct 2022, 19:04 David Rodrigues, <david.proweb@gmail.com>
> wrote:
>
>> Hello!
>>
>> I'm converting my code to use short closures where possible, and I ran
>> into
>> a problem using compact().
>>
>> Basically, the names used in compact() cannot be accessed due to a bug,
>> reported in 2019 still in PHP 7.4 (ID 78970).
>>
>> https://bugs.php.net/bug.php?id=78970
>>
>> It seems to me to be a reasonable problem and one that needs attention, as
>> the message is not that "compact cannot be used here", but that "the
>> variable does not exist".
>>
>> The code below may reproduce the problem:
>>
>> $x = 123;
>> (fn() => compact('x'))();
>>
>> https://3v4l.org/AFARs
>>
>> Is there any possibility of this being fixed? I would love to help, but I
>> don't have much C programming skills, unfortunately.
>>
>
> I'd rather hope for compact() to finally be deprecated and
> targeted for
> removal 😛
>
> The fact that it still exists precludes (or at least complicates) future
> optimization of scope + inlining in the engine.
>
> Similar thoughts towards extract(), I'd say.
>
>