phpstorm.dev

PHP ecosystem insights

PHP knowledge

Versioned PHP knowledge with primary sources and explicit verification dates.

Source checks through 2026-10-27 · 57 sources · This published snapshot is over 48 hours old.

Verified 2026-10-24 draft Permanent entry

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 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

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.