Scope: an operation identity for long-running PHP (proposal)
Under discussion; not accepted. The RFC page, version 0.9.3 (dated 2026-10-27, last modified 11:51 UTC, author Frode Børli), reads Status: Under Discussion, with the discussion thread linked and the voting thread "tbd". The official RFC index collected at 17:01 UTC on 27 October also lists it under discussion (index date 2026-10-23). Version 0.9.1 (26 October) had read Draft (which edit changed the header was not recorded) although the author had already started the discussion on internals at about 16:04 UTC that day, so the earlier conflict between the page header and the thread is resolved. RFC karma was requested on 13 October and granted on 25 October. The proposed version is "Next PHP 8.x (PHP 8.7)".
Proposal
The RFC adds a final class Scope that identifies a unit of work, such as an HTTP request, queue job or CLI command,
for servers that handle several, possibly overlapping, operations in one process:
new Scope(),$scope->run(callable $callback, mixed ...$args): mixed(makes the scope current while the callback runs and restores the previous one afterwards, also on exceptions),$scope->bind(Closure $closure): Closure(returns a closure that always runs in that scope),Scope::getCurrent()andScope::getRootScope().Fiber::getScope()andGenerator::getScope(). A fiber or generator belongs, for its whole life, to the scope that was current when it was created, wherever it is later resumed from. Ordinary closures do not capture a scope.- Code outside any
run()is in the root scope: one per PHP request (the whole process under the CLI).getCurrent()always returns an object, so a scope can always be aWeakMapkey; the RFC expects libraries to keep per-scope data in their ownWeakMaps. - A scope is an identity only: it does not hold values, wait for fibers, cancel work or end when
run()returns. It lives while something references it.
The RFC contrasts this with per-coroutine state in the separate Concurrency Support RFC, which it says could assign its
coroutines to scopes, and cites Java ScopedValue, .NET ExecutionContext/AsyncLocal<T>, Python contextvars, Go
context.Context and Node AsyncLocalStorage as prior art.
Votes as proposed
- Primary (2/3): add
Scope,Fiber::getScope()andGenerator::getScope(). - Secondary A (50%+1, counted only if the primary passes):
$_GET,$_POST,$_COOKIE,$_FILES,$_REQUEST,$_SERVERand the session follow the current scope; a new scope starts empty and the root keeps the SAPI's values.$_ENVis excluded. (The 13 October karma request had said$_SERVERwould stay shared; version 0.9.1 includes it.) - Secondary B (50%+1, independent of A): the default locale (
Locale::setDefault()) and default timezone (date_default_timezone_set()) follow the scope; a new scope starts from the ini defaults.
Output buffers, error handlers and ini values are left for later RFCs. No vote dates are set.
Version 0.9.3 adds one rule to vote A: session_start() throws a RuntimeException, instead of blocking the process,
when a locking save handler's session ID is already active in another scope of the same process (ext-scope 0.4.4). The
RFC lists this as its only open issue; the alternative named there is to let both scopes share one $_SESSION and one
lock, which avoids the error but gives up vote A's isolation. Version 0.9.2 rewrote the introduction without changing
the proposal, and an implementation note on 27 October says ext-scope 0.4.3 was tested on ZTS builds with threads
(the parallel extension).
Compatibility and implementation
The only listed BC break is that a global class named Scope can no longer be declared; namespaced classes such as
PHPStan's PHPStan\Analyser\Scope are unaffected. The RFC's own sample found a global Scope class in 3 of 100 GitHub
PHP files; that sample was not reproduced here. Code that never creates a scope is described as behaving as today.
The reference implementation is the fiber-interop/ext-scope extension (0.4.2, PHP 8.2–8.5, installable with PIE),
which implements both secondary votes behind scope.superglobals and scope.locale_timezone ini switches, and a userland
polyfill fiber-interop/scope (0.4.0). A php-src pull request is planned before the vote. The extension, its tests
and the polyfill were not checked here.
Limitations
This summarises versions 0.9.1–0.9.3; the design can change. The middle of the 0.9.3 page (about 27,000 characters) was not re-read, so only the changelog-listed changes are recorded for it. Whether vote A's per-scope superglobals are sound for all SAPIs and event loops is the subject of the discussion, not established fact.
Related: karma request coverage in the 25 October digest; discussion start in the 26 October digest.