Code that opens a file or starts a database transaction needs the same try/finally around it every time, so the cleanup runs even when something fails. Larry Garfield and Arnaud Le Blanc's RFC adds a using expression that moves that setup and cleanup into one reusable function. It's a second take on their earlier Context Managers RFC, borrowing the idea from Python.
Show me
A context manager is an ordinary generator. Everything before yield runs when the block starts, the yielded value goes into the block's variable, and everything after it runs when the block ends:
function timer(): \Generator { $start = microtime(true); yield; return microtime(true) - $start; } $time = using (timer()) { something_slow(); };
Because using is an expression, it evaluates to whatever the generator returns, here the elapsed time. That's also why the block ends with a semicolon.
The common case wraps the block in try/finally. Exceptions thrown inside the block pass through the generator, so its catch and finally blocks see them. This one is based on the RFC's transaction example:
function transaction(PDO $db): \Generator { $db->beginTransaction(); try { yield $db; } catch (\Exception $e) { $db->rollBack(); throw $e; } $db->commit(); } using (transaction($db) => $conn) { $conn->exec("UPDATE accounts SET balance = balance - 100 WHERE id = 1"); };
How it works
- One yield. A generator that yields a second time throws an
Error. - Same scope. Unlike a closure, the block runs in the surrounding function, so it can use its variables without
use. Areturninside it returns from that function, and the generator is resumed once so its cleanup code still runs. - Cleanup of the variable. When the block ends, PHP unsets the variable after
=>. - Several managers at once.
using (foo() => $a, bar() => $b) { ... };nests them. try using. You can writetry using (manager() => $var) { ... } catch (...) { ... }to catch what the manager rethrows.- Resources. A resource such as the result of
fopen()is wrapped automatically in a manager that closes it, sousing (fopen('foo.txt', 'r') => $fp) { ... };closes the file when the block ends. - Objects. A class can implement the new
ContextManagerinterface, whosegetContextManager()method returns the generator.using (new Timer() => $t)then works like calling that method yourself.
The RFC's other examples include file locks, temporarily swapping the error handler, and scopes for async code that wait for or cancel their tasks.
What changed from version 1
The first RFC defined a context manager as an object with two methods. The authors say PHP's type rules made the Python version of that idea impractical, and they couldn't find a workaround everyone accepted. A generator side-steps that and also gives using a value to return. The cost is typing: PHP can't declare what a generator yields or returns, so you'd rely on static analysis annotations like @return Generator<int, string, null, int>.
What it means for existing code
using becomes a semi-reserved keyword. You can't name a global function or constant using any more, but methods and class constants with that name keep working.
Where it stands
Larry Garfield announced the RFC on internals on November 5, 2026 (thread), targeting PHP 8.7. Arnaud Le Blanc's implementation is linked, though he warns it's only partly complete. Early replies focus on the syntax: Stephen Reay and Lazare Inepologlou asked for as instead of =>, to match foreach. Larry said version 1 used as, but many people felt it implied the left side becomes the variable. One question is still open: what a using with several managers should return. Acceptance needs a two-thirds majority. No vote has been scheduled yet.