Re: [RFC][Discussion] use construct (Block Scoping)

From: Date: Tue, 04 Nov 2025 18:19:39 +0000
Subject: Re: [RFC][Discussion] use construct (Block Scoping)
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-129075@lists.php.net to get a copy of this message
On 3 November 2025 21:46:15 GMT, Seifeddine Gmati <azjezz@carthage.software> wrote:
>Hello internals,
>
>Tim and I would like to open the discussion on our new RFC that we've been
>working on: "use construct (Block Scoping)".


Hi both, 

I agree with Ed and with Arnaud: this feels like it's trying to squeeze two different features
into one syntax and ends up with an awkward version of both. 


For what Python calls "context managers", it offers very little: the programmer is still
reliant on reference counting and cycle collection to actually clean up the resource, and objects
can't directly interact with the context life cycle.

Python in particular has a very carefully designed solution, and the PEP is well worth reading:
<https://peps.python.org/pep-0343/> I think
most of that could be directly ported to PHP.


For block scoping of "normal" variables it feels clunky to add an extra block, rather than
declaring the variable with a keyword like "let" or "var". This is particularly
obvious in the foreach example, where the variable has to be named twice on one line:

use ($value) foreach ($array as &$value) {

Languages with a keyword for declaring variable scope instead let you write the equivalent of this:

foreach ($array as let &$value) {

I have said before that an opt-in block scope would solve my main concern about automatically
capturing variables in closures, because you could write this to make scope explicit: 

$foo = fn() {
    let $localVar;
    something($localVar, $capturedVar);
    something_else();
}

With this proposal, that would again be rather verbose: a mandatory extra set of braces, to put the
scope inside the closure: 

$foo = fn() {
    let($localVar) {
        something($localVar, $capturedVar);
        something_else();
    }
}


I think splitting the two use cases (context managers and scoped variables) would allow us to have
much better solutions for both.



Rowan Tommins
[IMSoP]


Thread (42 messages)

« previous php.internals (#129075) next »