Context Managers v2 (generator-based using) proposal
Draft; not accepted. The RFC page was created on 2026-10-23 (wiki change feed 22:34 UTC, last modified 22:36 UTC). It is version 2.0, dated 2026-10-23, by Larry Garfield and Arnaud Le Blanc, with status Draft. The proposed version is PHP 8.7. The discussion thread and implementation fields are empty and the voting thread reads "tbd". When first read, the page title was the template placeholder "PHP RFC: Your Title Here". After an edit at 19:43 UTC on 24 October (by crell) it reads "PHP RFC: Context Managers v2". The status is still Draft, the discussion thread and implementation fields are still empty and the open issues are unchanged. Other parts of that edit were not compared line by line. The official RFC index collected at 22:01 UTC on 24 October lists "Context Managers, version 2" as a draft dated 2026-10-23 (it was absent at 04:02 UTC). The earlier Context Managers RFC (version 0.9, dated 2025-10-15, same authors) still says In Discussion in its header and in the index.
Proposed behaviour
The RFC adds a semi-reserved keyword using. It opens a block that guarantees setup and teardown work:
using (file_for_write('file.txt') => $fp) {
fwrite($fp, 'data');
};
- The expression before
=>must evaluate to aGenerator, which the RFC calls the context manager. If it is not aGenerator, anErroris thrown. Resources are the exception: they are wrapped automatically in a manager that closes them in afinallyblock. - The generator runs until its first
yield. The yielded value goes to the context variable. The body runs in the enclosing function's scope, and then control goes back to the generator. A secondyieldthrows anErrorat runtime. When the block ends, the context variable is unset. - An exception thrown in the body passes through the generator, so its
catch/finallyblocks can handle it. Areturnin the body returns from the enclosing function, but the generator'sfinallystill runs. usingis an expression. It evaluates to the generator's return value, ornullif the generator returns nothing. A single-statement body can be written without braces.- Several managers can be listed in one
using(using (foo() => $foo, bar() => $bar) { … };), which is the same as nesting them.try using (…) { … } catch (…) { … }is shorthand for atryaround the block. In that form theusingreturn value is discarded.
The RFC's examples include database transactions, flock() locking, suppressing listed exceptions and asserting
that an exception is thrown in tests.
Difference from version 1
Version 1 defined a context manager as an object with two methods. The v2 RFC says PHP's type handling made the
Python idiom impractical and that "no generally acceptable workaround was discovered". Generators avoid that problem
and also allow uses other than try/catch. The cost is weak typing: a generator function can only declare \Generator
as its return type. The RFC points to the Generator<TKey, TValue, TSend, TReturn> docblock notation that static
analysers already support.
Open issues and limitations
- Open issues (from the RFC): with several managers, should
usingreturn the outermost result or an array of all results? Should an optionalContextManagerinterface with agetContextManager(): \Generatormethod be added?send()support is left as future scope. - Value escape: if the body stores the context variable somewhere else, it stays reachable after the block. Each manager then decides whether to close the value (leaving a closed handle) or not (leaving it open). The RFC calls this intrinsic to PHP's memory management.
- Compatibility:
usingbecomes semi-reserved. It can no longer be used for global constants or functions, but existing methods and class constants keep working. - Vote: the page has one primary vote, "Implement generator-based context managers?", needing a 2/3 majority. No ballot is open and no discussion has started. The RFC links no implementation.
This page summarises a newly created draft. Syntax, semantics and the target version can change, and nothing here is
in released PHP. Part of the RFC page (about 4,600 characters in the middle) was read in a second pass with
--offset. The examples section was skimmed, not reviewed.
Related: IO hooks, Io\Terminal.