# [RFC] Context Managers, v2

- Thread: https://externals.io/message/133003
- Original archive: https://news-web.php.net/php.internals/133003
- Started by Larry Garfield on 2026-11-05T17:33:14+00:00
- Messages: 3
- RFC: Context Managers, version 2 (Under discussion) — https://externals.io/rfc/context-managers-v2.md
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #133003 — Larry Garfield — 2026-11-05T17:33:14+00:00

URL: https://externals.io/message/133003#133003

Hi folks.  Based on the feedback and problems with the previous Context Managers RFC, Arnaud and I have a new approach to present:

https://wiki.php.net/rfc/context-managers-v2

It basically swaps the two-method approach in favor of "it's a generator, you're welcome."  The result is, I think, both simpler and more flexible, and resolves a number of other issues as well.  More details in the RFC.

There is a linked implementation, though Arnaud warns it's only partially complete so go easy on it. :-)  But the design in the RFC is all ready for review.

-- 
  Larry Garfield
  larry@garfieldtech.com

---

## #133006 — Stephen Reay — 2026-11-05T18:18:19+00:00

URL: https://externals.io/message/133003#133006 · In reply to: #133003

> 
> 
> ﻿Hi folks.  Based on the feedback and problems with the previous Context Managers RFC, Arnaud and I have a new approach to present:
> 
> https://wiki.php.net/rfc/context-managers-v2
> 
> It basically swaps the two-method approach in favor of "it's a generator, you're welcome."  The result is, I think, both simpler and more flexible, and resolves a number of other issues as well.  More details in the RFC.
> 
> There is a linked implementation, though Arnaud warns it's only partially complete so go easy on it. :-)  But the design in the RFC is all ready for review.
> 
> --
>  Larry Garfield
>  larry@garfieldtech.com
> 

Hi Larry, 

TLDR: why `=>` rather than `as`?

As a userland developer I like what this is trying to achieve. I'm not knowledgeable enough about the internals to comment on the finer details or their ramifications - I just have a bike shedding suggestion/question which seemed glaring to me so I wonder if it's because of an ambiguous syntax or some other issue?

The `=>` symbol seems odd to me, given how it's used elsewhere (generally as an indicator of a key/value pair, whether in an array or match block) and how generators/iterables are used elsewhere (ie foreach as)

Your example is:

using (file_for_write('file.txt') => $fp) {

Whereas to my eye this seems like a much more obvious choice:

using (file_for_write('file.txt') as $fp) {

Anyway, just a thought. 

Cheers

Stephen

---

## #133007 — Larry Garfield — 2026-11-05T18:30:11+00:00

URL: https://externals.io/message/133003#133007 · In reply to: #133006

>
> Hi Larry, 
>
> TLDR: why `=>` rather than `as`?
>
> As a userland developer I like what this is trying to achieve. I'm not 
> knowledgeable enough about the internals to comment on the finer 
> details or their ramifications - I just have a bike shedding 
> suggestion/question which seemed glaring to me so I wonder if it's 
> because of an ambiguous syntax or some other issue?
>
> The `=>` symbol seems odd to me, given how it's used elsewhere 
> (generally as an indicator of a key/value pair, whether in an array or 
> match block) and how generators/iterables are used elsewhere (ie 
> foreach as)
>
> Your example is:
>
> using (file_for_write('file.txt') => $fp) {
>
> Whereas to my eye this seems like a much more obvious choice:
>
> using (file_for_write('file.txt') as $fp) {
>
> Anyway, just a thought. 

The v1 RFC originally used `as`, which is what Python uses as well.  However, a lot of people were concerned that it implied that the left side expression would be "used as" the right side variable.  While in some cases it might be, the typical case is that it's not.  The context variable is emitted by the context manager, but is not the same thing.

At the time, no one could really agree on a better keyword.  But => was right there waiting to be used, and already has an "evaluates to" or "yields" or "produces" meaning, which is roughly accurate to what happens here.

I don't mind `as`, personally, but the consensus before seemed to not care for it.

--Larry Garfield
