Re: [RFC] Scope

From: Date: Mon, 16 Nov 2026 15:00:00 +0000
Subject: Re: [RFC] Scope
References: 1 2 3 4 5  Groups: php.internals 
Request: Send a blank email to internals+get-133107@lists.php.net to get a copy of this message
On Sat, Nov 14, 2026, at 4:13 PM, Frode Børli wrote: > Hi Larry, Côme, > > Two days ago I changed the RFC so that a new scope starts as a copy of > its creator's state. Writing that rule out in full, for the extension > and for the text, has changed my mind back, and I want to state my > position plainly before the text moves again. > > The copy rule looks like one rule but is not. The six arrays copy with a > reference count, but an object is a handle, not a value: > $_SESSION['user'] holding an entity, or $_GET replaced by an ArrayAccess > object, which PHP allows, is the same instance in both scopes, and a > write inside the new scope reaches the creator. That is the leak a scope > exists to prevent. The session has no copy at all: it is a store with a > lock, and specifying a copy of it meant choosing between writing under a > lock the copy does not hold, a snapshot whose writes vanish, or sharing. > Headers and output cannot be copied either. So "a new scope starts as a > copy" needs a table of what is copied, what is shared and what is > neither, and every later RFC adds a row to it. That is the mess, and the > one who pays for it is whoever creates a scope and has to look up, or > find out by trial, what came along and what did not. The object-reference question has occurred to me as well. However, that would occur just the same with a "manually-copied" approach. // This is dumb, but that doesn't mean people don't do it. $_GET['user'] = new User($_GET['uid']); $_SESSION['user' ] = new User($_GET['uid']); // ... $get = $_GET; $post = $_POST; $request = $_REQUEST; $server = $_SERVER; new Scope()->run(function () use($get, $post, $request, $server) { $_GET = $get; $_POST = $post; $_REQUEST = $request; $_SERVER = $server; doStuff(); }); Both of the saved user objects will be shared across the scope boundary, for the same reason as if it were automated: Shallow copies. Even if you argue that no one is going to copy $_GET like that in practice (and I suspect they will), $_SESSION is still a question. And, again, scopes can and will be used for things other than requests. An empty-start approach effectively means that legacy code that reads from ambient state is inherently unsafe, because depending on how it is run the ambient state may be missing. One could argue that legacy code that reads from the ambient state is already unsafe and it shouldn't be doing that, and I'd likely agree, but that doesn't mean there aren't a few million lines of such code in the wild already. > The clean rule is the one the RFC had until 0.9.5: a new scope is a > clean slate for the state that follows the scope. It has the state the > engine has immediately before the request line is parsed: no query > string, no $_POST, no $_SERVER, no session, locale and timezone at their > php.ini defaults, and the same for whatever a later RFC moves into the > scope. What does not follow the scope is unchanged: $GLOBALS, class > statics and static locals are the application's, loaded once and shared > by every scope, as globals are in every language with such a construct.. > It is not a sub scope that inherits some things from the scope that > created it. It is a new scope, as if there had been no request before, > and whoever creates it puts in it what they want, explicitly. That is > one sentence for the request state, it defines "fresh" for every kind of > it at once, now and later, and it leaves the programmer in control of > what is included and how. > > On the landmine: I would rather have the one who creates a scope > discover loudly that $_GET and $_POST are gone than have some things > present and others not. A scope is not something code falls into; the > only one who can run your function in a scope is your caller, who can > clear $_GET before calling you today. Creating a scope is a deliberate > act with a documented meaning, and a loud failure on the first run is a > better outcome than a shared object quietly written to from two scopes. The challenge is that the person starting the scope, the person who wrote the code reading from ambient state, and the person calling the code that uses ambient state *are three different people*. And there could easily be multiple people in that stack, too. So the person creating the scope frequently will not know that there's ambient-state reading going on, or to even look for it in some transitive composer dependency. So yes, an arbitrary function can "fall into" a scope that it never expected, written by someone else a decade after the original code was written. Perhaps I am over-exaggerating how often that particular chain will happen, but I don't think so. Not by much, anyway. > That is the version I am comfortable implementing, and it is what the > released extension does; the next version of the RFC returns to it > unless this discussion shows me wrong. If someone wants a more > intertwined Scope, where a new scope inherits part of its creator's > state, I would like to hear how, what exactly is inherited and why, > including what happens to objects and to the session, because I could > not make it hold together. Alternatively, if the list prefers that > direction, I am willing to withdraw the RFC and let someone else take it > over. Either way I will keep implementing the minimal, clean version in > fiber-interop and Swerve. > > Best regards, > Frode Børli I'd like to hear input from other core devs before a decision is made; so far I'm the only one that's really gone deep on it. I suspect the more engine-aware people are still busy defending php-src from the DDOS attack of AI bots filing security issues, but I would still like to hear from them. Possible thought: would it be feasible to "cluster" the various ambient state bits and have opt-in/out flags just for those coarser-grained sets? For instance, I honestly can't think of a use case for the *output* elements to be shared/copied. Those, I think, are safe to have always-empty.. For anything ini-derived, there's always still a fallback: The ini value, so copying those from the creator scope is likely unnecessary. The issue I am concerned about is, I think, mostly relevant for the superglobals. That's a much smaller scope (no pun intended) to figure out how to address. I agree that session is the tricky one; I'm not sure what the optimal design is for that. But I think we can do better than "all or nothing." Would that smaller surface area make for an easier implementation without compromising legacy safety? Also, tangentially: From an ergonomics standpoint, is there a way to leverage the proposed using syntax with scopes? It seems like a natural fit, in the sense of guaranteed "wrapping" around some operation. It could be kinda weird to introduce using and an old-style "pass a single callable" feature in the same release. --Larry Garfield

« previous php.internals (#133107) next »