Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125715 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 qa.php.net (Postfix) with ESMTPS id D26EB1A00BD for ; Tue, 1 Oct 2024 19:07:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1727809755; bh=3NM/Oi0isa7FlMvh3SORLC77uU4KeyklVp1d3HWzgjg=; h=Date:From:To:In-Reply-To:References:Subject:From; b=lKTZIIYx81BxniQ8nAoF/ggbvCuNJ9YmI0/7JMSgy7Cf9+j/PCV//dRl9IMKz1fiS 5f1Rm5iuQoBYXaqKaezwEa48BwGIWy9FyIJBhSPSOYwjczAa1yZYRJXLvEWH6/2XUp n/aU663LHK5HcZWPTb2ASPF2GUBHvTZ1Ex4GYlzFAUdX3bJF6lDIqK3EDQr9oab8S+ scpfQcwFI1AKP70ZSvdPArbQoNCHk6IaffenoqtuTvBJLFkse67cZJSN01n9fikTQ+ Qzbx+12TEKNoSuPuySVOg79TCdxknI1SY3+3OuREAhntXhgPASxnX5MAJtL33f1Pxs KyQpm61ZrymVw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C521E18007A for ; Tue, 1 Oct 2024 19:09:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-0.1 required=5.0 tests=BAYES_50,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.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a5-smtp.messagingengine.com (fhigh-a5-smtp.messagingengine.com [103.168.172.156]) (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 ; Tue, 1 Oct 2024 19:09:14 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfhigh.phl.internal (Postfix) with ESMTP id ED3DA1140CF3 for ; Tue, 1 Oct 2024 15:07:00 -0400 (EDT) Received: from phl-imap-10 ([10.202.2.85]) by phl-compute-05.internal (MEProxy); Tue, 01 Oct 2024 15:07:00 -0400 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=fm2; t=1727809620; x=1727896020; bh=vT8KPkDyaSlWYeuE6lCkWICU35hiBo3lMGwViFPzuZ4=; b= gLAvqVFsNncURZVlh94qTN0Gq9gYkSKMWJawuyrgACh0W9JVM/o5i+RtooM4XYI8 5oP1KCGnXM01Jgz7DPfWxov2kvxE08WG/Nd54FQQZV4wspeH3er+tTqrW5qP/Yzt YrpxG2K9BythuUV4N0+Pzy719OYbIAFCC3pb8JOJZadnDSM4MQODmD+ium0jp0Lz sIL8+fROtXKIQ9yrcw/Lyjo94EffJjcXu3k+bnwWZ96E880pNTJNiw6PfIVc7Wp+ UfrmPAWu5YJnTyy/+fW5prplZR41tXPIL63fbGS9E3D4tnQTeXJao+zLrBczT0Sz cQWhlgA2H7CtOkTBRvBluA== 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-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1727809620; x= 1727896020; bh=vT8KPkDyaSlWYeuE6lCkWICU35hiBo3lMGwViFPzuZ4=; b=H 3egagEC2S3rlPBnXi7eUrJoUOlqodIr6KZSPhtKB3w5hHlxo7vvWsS4K58Ez3UxF Z2uE+MaNuG3c4gXwHID8iwQO4FYqwunQczkshkhvF4wNelNLKY2q/aie5yDMSVo+ Rietk65l6TEb9MjpdoKHcyFKMrSB9QwB9cPwAk9PvWATFFQ1TdPa/s8SrgKU+YPO 40ZSTpsDXGQpUdy8hu2qwVr/E9zpZ5MIs5Fja9OtD7eU+UeopmRU3foTcLAc/ZV6 6nXw9Mwnj1YKzAuJJ8/DKq7S4gGReTbIQIXt8CHf5tShpJJAgIzg0eaNTG26s/qK tWsXSC4BmCV/1sp9jWyXA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddujedgudeftdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepofggff fhvffkjghfufgtgfesthejredtredttdenucfhrhhomhepfdftohifrghnucfvohhmmhhi nhhsucglkffoufhorfgnfdcuoehimhhsohhprdhphhhpsehrfigvtgdrtghordhukheqne cuggftrfgrthhtvghrnhephedutdekiefhkefhtedvueevvedukeeglefhtdefueffhfeh tedtheehgfekudefnecuffhomhgrihhnpehphihthhhonhdrohhrghdpmhhitghrohhsoh hfthdrtghomhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhr ohhmpehimhhsohhprdhphhhpsehrfigvtgdrtghordhukhdpnhgspghrtghpthhtohepud dpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishht shdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: id5114917:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 977103C0066; Tue, 1 Oct 2024 15:07:00 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Tue, 01 Oct 2024 20:06:07 +0100 To: internals@lists.php.net Message-ID: In-Reply-To: <5cd59815-99e0-4d69-8ce1-fa84b4d255dd@app.fastmail.com> References: <0a45288d-8ae1-4b1c-8836-6bdd1cf34eb2@gmx.de> <5cd59815-99e0-4d69-8ce1-fa84b4d255dd@app.fastmail.com> Subject: Re: [PHP-DEV] Protected destructors Content-Type: text/plain Content-Transfer-Encoding: 7bit From: imsop.php@rwec.co.uk ("Rowan Tommins [IMSoP]") On Tue, 1 Oct 2024, at 19:29, Larry Garfield wrote: > I would have said with() would be neat in PHP. :-) I have been considering for a while proposing Context Managers [Python's with(), not to be confused with VisualBasic & JavaScript unrelated feature with the same keyword]. My primary example use case is safe database transactions, which I've seen implemented in PHP in two ways: 1) Callback style, where the code to run in a transaction has to be wrapped in a function, usually an anonymous closure. This is often cited as a use case for implicit capture in closures, but even with that it adds a layer of indirection, and changes the meaning of "return" and "yield" inside the wrapped block. 2) "Resource Acquisition Is Initialization" style, where the destructor rolls back the transaction if it hasn't been committed or rolled back manually. This requires fewer changes to the wrapped code, but as Arnaud points out, it's not 100% reliable / predictable in PHP, due to details of the GC. Context Managers present a third option, where the code in the transaction remains a normal sequence of statements, but there is a more explicit guarantee about what will happen when the with{} block is exited. The Python design document has interesting background on what they included and excluded: https://peps.python.org/pep-0343/ C#'s "using statement" is similar, but explicitly designed for ensuring the correct "disposal" of an object rather than hooking entry to and exit from a "context": https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/using Regards, -- Rowan Tommins [IMSoP]