Re: [RFC] Context Managers
| From: | Deleu | Date: | Tue, 16 Dec 2025 00:21:48 +0000 |
| Subject: | Re: [RFC] Context Managers | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-129611@lists.php.net to get a copy of this message | ||
On Mon, 15 Dec 2025 at 20:01 Ben Ramsey <ben@benramsey.com> wrote:
>
>> Since the only feedback on what to use for "as" was that => makes sense,
>> we have changed the RFC to use => instead. So the new syntax is
>>
>> using (new CM() => $cVar) {
>> // Do stuff here.
>> }
>>
>
> Going to be controversial here, but this is confusing, because it operates
> in the exact opposite of every other usage of => we have. With associative
> arrays, the left is assigned to the expression on the right; with arrow
> functions, the return value is the expression on the right; with match, the
> expression on the right is returned.
>
> This is going to be easy to get wrong.
>
>>
> I agree with Matthew.
>
> I think it makes more sense to reverse them, like this:
>
> using ($cVar => new CM()) {
> // Do stuff here.
> }
>
> I think it’s still clear what this is doing, when reading it.
>
> Cheers,
> Ben
>
I also agree with Matthew but the reversed proposed here looks very very
awkward to me. I think the most natural thing is the “as” but I may have
missed the discussion on why it had to be changed.
Thinking of foreach ($array as $value), an item from the array (left) is
assigned to $value (right). That seems symmetrical to using (new Manager as
$manager) where the instance (left) is assigned to the variable (right).
Also when using aliases on the top of the file “use Foo as Bar;” as is also
assigning the left to the right.