Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129780 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by lists.php.net (Postfix) with ESMTPS id 0C5BA1A00BC for ; Sun, 18 Jan 2026 13:07:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1768741635; bh=5Px0yTjiYRyeaiUbIlTU7POPfhCmQCkwnAhVByTf11Q=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=IF27H8ETBgg63zQKLWNbdx0ssV6ykM9d5oIR1xmLypzLEb4m4Nw/RCIUpfdpumU/v EHnLAii5RtpyRb4J+l1sZ7K6sAqehkQojYup50/7Q236cyDJ2klqDckwowmsCfd9uV jWDyiaGd/Yb5wOaB0DntP8nWKBCG2/wJJxlONlZ5y0jfm7CG2EppVgTzS+CDX7Cx+O peM8hHwGopxI2vd63zMcrcmmnfu/+sbB/AqenD49tRbmaVRuRKxklM48P7WbxEmCHF H37amWSksNAhpzpr2xisZERlbI3QMIjS5Cb6NMYSe674SYR08RFFT3b47vboKfAo18 DmLDK9K4t6fXg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 2216B180068 for ; Sun, 18 Jan 2026 13:07:15 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-25) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from chrono.xqk7.com (chrono.xqk7.com [176.9.45.72]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Sun, 18 Jan 2026 13:07:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1768741623; bh=RoVH5oUpQGzub9Zxyu61CUUMCywXYEX3uXFYooliPyo=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type:from:to:cc:subject:message-id; b=lx7Vtrd15gH910MdBDnMtWwRTwwyyOSvFOuLfZfqjkIcI8rOGU801UmXBPEbIPplN zpFHuukbBcXasQv4kwsYFvjtphDd6NnEl4qT90tsXrVSccEJ7j2oswPxuH5qWd2Vk5 PlqEnzzPmW3u8lbh78f4DuiyKUCiNxkBDwvNBFVdxFxbAwCmeWPNjo95F8X3iV2g51 iaIdbpC3txtRiAP1su8V3FBgZJf+qKZsccGxtGuQaqq5iKN+mDKTXArxEz8FGjrZtK jdtdb3g6bu+w/nxfCIwx6Iu9T9PGtLhy+NUd2AJdipATDzFcTYPWEkI63v9VGekPL5 Zl7RhVHF06Ouw== Message-ID: <011681dd-cfdd-4634-a3ed-f49e73cc2ccd@bastelstu.be> Date: Sun, 18 Jan 2026 14:07:02 +0100 Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Subject: Re: [PHP-DEV] [RFC] Context Managers To: Volker Dusch , Larry Garfield Cc: php internals References: Content-Language: en-US In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: tim@bastelstu.be (=?UTF-8?Q?Tim_D=C3=BCsterhus?=) Hi On 1/15/26 19:38, Volker Dusch wrote: > a function is already a reasonable scope That - together with the other points you raised - made me realize one thing: In contrast to block scoping, the main purpose of “Context Managers” is *not* managing arbitrary variables in the current scope in combination with existing control structures. Instead it is just managing a single variable in a reasonably *self-contained* fashion, with other variables just needing to “exist”. It could therefore also just be a regular function taking a callback, as is already done in userland, e.g. with Laravel's DB::transaction() helper. I believe something like this would be equivalent to the RFC: function using( callable $runInContext, ContextManager ...$managers ): void { $contexts = []; foreach ($managers as $manager) { $contexts[] = $manager->enterContext(); } try { $runInContext(...$contexts); foreach (array_reverse($managers) as $manager) { $manager->exitContext(); } } catch (Throwable $e) { foreach (array_reverse($managers) as $manager) { if ($manager->exitContext($e) === true) { $e = null; } } if ($e !== null) { throw $e; } } } This would completely side-steps the “break to exit” question, since a `return` will just work. It also avoids introducing new keywords, it just requires a new function in the global namespace. I understand that using variables from the current scope in a Closure is currently not particularly convenient, particularly when they need to be changed. However this is something that can be solved in a generic fashion, for example inspired by the C++ lambda syntax: // Captures everything by value and $result by reference. function ($context) use (*, &$result) { } // Captures everything by reference. function ($context) use (&*) { } which would also be useful in other situations. Best regards Tim Düsterhus