Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129152 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 9DD441A00BC for ; Sat, 8 Nov 2025 19:54:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1762631678; bh=NzweRDS9v6fdduyCETWn1EaHvT+4wxOUprWHFrZOwfo=; h=Date:Subject:To:References:From:In-Reply-To:From; b=V1Etzdi4qgh2vMYUUIy5ggdJI5KmhzIjFquANUxhQIiGBOWiCI+/q8y3d21d7Ek1V jDF5/2cVPV36BcK0hJqSL/O0d0AO0Tvyzho1CYOqH8S+/Taf3JweloHOhiDc1sZQ3W gjC1kwkaYFdWSXLish34M6ldX6hCR4juf+qSGXgHbuDCX8HHVngXzgu8kBFw0VjD3w BbuUvUTaBMgc68FigVSayxHjPesSXSHmyW4ybidamONbeGhm57zyqTEADgzGR6Rpdw /lUfF1dfZiA36hbV+Jyk/6m3I1mq+vvNwjy/qQCUuTLmWaUjl7gGKdUKBZ73XEZspu o49trP9Q+HFDg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id F1D62180050 for ; Sat, 8 Nov 2025 19:54:36 +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=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=4.0.1 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-b1-smtp.messagingengine.com (fhigh-b1-smtp.messagingengine.com [202.12.124.152]) (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 ; Sat, 8 Nov 2025 19:54:36 +0000 (UTC) Received: from phl-compute-09.internal (phl-compute-09.internal [10.202.2.49]) by mailfhigh.stl.internal (Postfix) with ESMTP id AFFBB7A0167 for ; Sat, 8 Nov 2025 14:54:30 -0500 (EST) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-09.internal (MEProxy); Sat, 08 Nov 2025 14:54:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rwec.co.uk; h=cc :content-transfer-encoding:content-type:content-type:date:date :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm3; t=1762631670; x=1762718070; bh=gFWH1oslfbqj1pCspYgjSYqW6B/6DmoysREWqgGNrbc=; b= aw/Y1IrzV8DwW4za+7XDEbj6waWiYXnJpoTo3by9YYNwBVcxJRm6IggtHw5ON6rt GoHhUq5GBnGbtQqC9X/WEcjwor4cMVVSDajJ4zqmuGoyNwA5Jya+uiyXLKCJitcZ xsXviw1QcI+R2GK8iK/o09inouUEkdjSvHcRtlhPvCI2RJEsMnEHcrFoyMTQPIYX Zbhl97GBKiwVv6/34Zf9TIPEol1qUVyJeQHWZDolAlq2SjuaQxIYprhSD/cY1iDo CfsbTQJCtg8lCmRhsUZ+lSm0C2/FF7NLNZQ3vdwSNpBJqH5bUSnRBWKGUxddZO/0 oged6W1RMoFnb04Zpkqofg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1762631670; x=1762718070; bh=g FWH1oslfbqj1pCspYgjSYqW6B/6DmoysREWqgGNrbc=; b=nKg8wReu35yA5Poye qCDKesNQkyCKsRWpLJ3oxuXAkPzhCjd4m/JficTM7enjfaBXg8Xpub3oxLHj1jhx CZgr5tIfxa3UY9pv37eb5lWtS/jWhvcEm0S3gs5wrK0RkkcyDIXuZRffzG0Aj6c4 dM5mUzkk81bGGmSXhJBDfQ3E/X+TgChlgl/WPgjS9Wy/zbJhXXhwzZz3qI3NVSlP G5typp0CZFwN6xB7Ku3fUPw+N5JEe6uBzJ/fPyR07FHtelVi2r3hX8axdTDkjpWt CNDdon2P3vTmXWdd29hwlMAu3DyHrG6WII8dBB1ausYZaRlmqeMgm6KSkwyIMMGa U1ReQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdduleefgeefucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfurfetoffkrfgpnffqhgenuceu rghilhhouhhtmecufedttdenucenucfjughrpefkffggfgfuvfhfhfgjtgfgsehtkeertd dtvdejnecuhfhrohhmpedftfhofigrnhcuvfhomhhmihhnshculgfkoffuohfrngdfuceo ihhmshhophdrphhhphesrhifvggtrdgtohdruhhkqeenucggtffrrghtthgvrhhnpeffke evudffuddvheejvdefkeelfedtudegfeehjeduheegieduffeggeegveefheenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehimhhsohhprdhphh hpsehrfigvtgdrtghordhukhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphho uhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA for ; Sat, 8 Nov 2025 14:54:29 -0500 (EST) Message-ID: <864235f2-41ea-4ea5-ae45-9fdb6b6c5a6c@rwec.co.uk> Date: Sat, 8 Nov 2025 19:54:28 +0000 Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PHP-DEV] [RFC] Context Managers Content-Language: en-GB To: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On 05/11/2025 22:38, Bob Weinand wrote: > The choice of adding the exception to the exitContext() is > interesting, but also very opinionated: > > - It means, that the only way to abort, in non-exceptional cases, is > to throw yourself an exception. And put a try/catch around the with() > {} block. Or manually use enterContext() & exitContext() - with a fake > "new Exception" essentially. > - Maybe you want to hold a transaction, but just ensure that > everything gets executed together (i.e. atomicity), but not care about > whether everything actually went through (i.e. not force a rollback on > exception). You'll now have to catch the exception, store it to a > variable, use break and check for the exception after the with block. > Or, yes, manually using enterContext() and exitContext(). The Context Manager is *given knowledge of* the exception, but it's not obliged to change its behaviour based on that knowledge. I don't think that makes the interface opinionated, it makes it extremely flexible. It means you *can* write this, which is impossible in a destructor: function exitContext(?Throwable $exception) {     if ( $exception === null ) {         $this->commit();     } else {         $this->rollback();     } } But you could also write any of these, which are exactly the same as they would be in __destruct(): // Rollback unless explicitly committed function exitContext(?Throwable $exception) {     if ( ! $this->isCommitted ) {         $this->rollback();     } } // Expect explicit commit or rollback, but roll back as a safety net function exitContext(?Throwable $exception) {     if ( ! $this->isCommitted && ! $this->isRolledBack ) {         $this->logger->warn('Transaction went out of scope without explicit rollback, rolling back now.');         $this->rollback();     } } // User can choose at any time which action will be taken on destruct / exit function exitContext(?Throwable $exception) {     if ( $this->shouldCommitOnExit ) {         $this->commit();     } else {         $this->rollback();     } } You could also combine different approaches, using the exception as an extra signal only if the user hasn't chosen explicitly: function exitContext(?Throwable $exception) {     if ($this->isCommitted || $this->isRolledBack) {         return;     }     if ( $exception === null ) {         $this->logger->debug('Implicit commit - consider calling commit() for clearer code.');         $this->commit();     } else {         $this->logger->debug('Implicit rollback - consider calling rollback() for clearer code.');         $this->rollback();     } } -- Rowan Tommins [IMSoP]