Re: [RFC] Context Managers

From: Date: Tue, 14 Apr 2026 13:53:12 +0000
Subject: Re: [RFC] Context Managers
References: 1 2 3 4 5 6 7  Groups: php.internals 
Request: Send a blank email to internals+get-130630@lists.php.net to get a copy of this message
Hi Am 2026-04-08 00:27, schrieb Rowan Tommins [IMSoP]:
Personally, I'm leaning towards option 1 - the case for "break" feels weak to me.
I agree here. For the example use case that is provided in the RFC, I feel that the break; is making intent much less clear. When dropping the comment that makes sense in the context of an RFC, but not inside a program, we have.
    using(file_open('records.csv', 'rw') => $fp) {
        while ($line = fgetcsv($fp) {
            if ($line[0] === $record['id']) {
                break 2;
            }
        }
        fputcsv($fp, $record);
    }
And here I'm wondering what the “high-level” purpose of that break; is. I would either need to add a comment:
    using(file_open('records.csv', 'rw') => $fp) {
        while ($line = fgetcsv($fp) {
            if ($line[0] === $record['id']) {
                // If the record already exists, we are done with processing the file.
                break 2;
            }
        }
        fputcsv($fp, $record);
    }
or I can just use a self-explanatory variable:
    using(file_open('records.csv', 'rw') => $fp) {
        $found = false;
        while ($line = fgetcsv($fp) {
            if ($line[0] === $record['id']) {
                $found = true;
                break;
            }
        }
        if (!$found) {
            fputcsv($fp, $record);
        }
    }
Using a variable is also more easily extendible to a “if found then X else Y” situation. A goto would also be clearer in intent, because I can give it a name and thus it effectively serves the purpose of a simplified comment. It's also easy to find where the control flow jumps by scanning for the label, without need to manually count control structures (which is particularly complicated when not all of them use braces). In fact jumping out of multiple control structures is explicitly documented as one use case on the goto documentation at https://www.php.net/manual/en/control-structures.goto.php:
[…] a common use is to use a goto in place of a multi-level break.
Insofar I don't see how the RFC's claim of “goto is generally discouraged, as it is less structured than break or continue” is backed up by evidence.
    using(file_open('records.csv', 'rw') => $fp) {
        while ($line = fgetcsv($fp) {
            if ($line[0] === $record['id']) {
                goto record_found;
            }
        }
        fputcsv($fp, $record);
    }
    record_found:
Best regards Tim Düsterhus

« previous php.internals (#130630) next »