Re: [RFC] Context Managers, v2
| From: | Côme Chilliet | Date: | Tue, 10 Nov 2026 11:32:58 +0000 |
| Subject: | Re: [RFC] Context Managers, v2 | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-133053@lists.php.net to get a copy of this message | ||
Hello,
Le jeudi 5 novembre 2026, 18:33:14 heure normale d’Europe centrale Larry Garfield a écrit :
> 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.
I do not understand this sentence: «Additionally, it is legal for a Context Manager object to
return $this as the context variable if that is appropriate in the specific use case.»
If ContextManager::getContextManager returns $this, which Generator is used 🤔
The whole section «Mixing with try-catch» is also a mystery to me. I do not understand why
"using" would need a short syntax for try catch any more than any other keyword we often
put in a try/catch, nor what the explanations in there mean.
Is the semi-colon necessary after the closing '}' ? That reads weird, it’s not clear to
me why it’s not a single '}' like after an if or foreach or whatever.
Also, this semi-colon is missing in at least one example.
In the transaction example, $conn does not exist, I suppose both instances should be $this?
«A return statement inside the BODY will return from the function the code is currently executing
in. A finally block in the generator context manager will still be executed, but otherwise control
returns to the function's caller.»
This is also hard to get for me.
Can you detail what this would output:
function context(): \Generator {
echo "A\n";
yield "B";
echo "C\n";
return "D";
}
var_dump(
using (context() => $c) {
echo "E\n";
return "F";
}
);
Côme
Attachment: [application/pgp-signature] This is a digitally signed message part. signature.asc
Attachment: [application/pgp-signature] This is a digitally signed message part. signature.asc