Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125721 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 7E0131A00BD for ; Tue, 1 Oct 2024 23:35:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1727825876; bh=vmp3t0HqJTgOFkyxpcysifPAnfa9x1mqZv0TkeoxxP4=; h=Date:From:To:In-Reply-To:References:Subject:From; b=iEmLKUEMvh3p2/rR6APVfhWpZ8NegpYxFIxiT0qlkRiO+iLsnD/G/hvtTbZ71Slbd lXC8HW1kNkZBuBC5VC9DsTQne5iMYnLgJWBX6qgMUtRDUX9Ke8Zt0bGEcznaBSUA9q Iv8ozQ/bEnrFz8We56feR4FO9ZtRMemK9WKRihCYhc84TxenKjQJXA4cK73Db+p3Qi U2feOaYGFrvLifXKpAwgtSU+0R8DkYhWl6WUpNnq3AtGoDjRY94hUO5kssdsCOtVn8 WJv7jPoSWbA6QWQmU5/NgO6QFGfhlZ2vIWBN9NDOY09D017FPj+hVVCgvD46CD9G76 5bTTfkWpVE3Bg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 208AC1801DD for ; Tue, 1 Oct 2024 23:37:54 +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_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a8-smtp.messagingengine.com (fhigh-a8-smtp.messagingengine.com [103.168.172.159]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 1 Oct 2024 23:37:52 +0000 (UTC) Received: from phl-compute-01.internal (phl-compute-01.phl.internal [10.202.2.41]) by mailfhigh.phl.internal (Postfix) with ESMTP id C8234114060C for ; Tue, 1 Oct 2024 19:35:38 -0400 (EDT) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-01.internal (MEProxy); Tue, 01 Oct 2024 19:35:38 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; 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=1727825738; x=1727912138; bh=GoKQHhtGYaNEBgykVI2GJ 9mviJjgwhNyk86BEwxqxSw=; b=EI+smqf8q6qdeMYCPrD4puYZv/78jaf8p1G65 pF1IsQWI+xhGjdj78yZBJ9UBDLa8YH2z/lqxdwb1LPk52XUeSrevWC/aJploUB7/ o62Ltzn1MjRjVaYfVIEeqh955mWkavajpB7HRvYHtfQow7F4/v2Wc9BhaDCClZVt 89ijFSOhbadxF2rOr2zVHx2DPDzgpRkhO23zyp+5OIyrp02Z/2ea5EXrFoM+qa0C r5PRoF5jdCCEPVrqDbEn3FOaDqyP28nvT4RyI3jRSXQUlnc0OEKc4cYInuqvmFKt 0neoMt0r0hGPYaRhSgtzvIVrrHPg02M9wJxpF516tiDk1S9lA== 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=1727825738; x= 1727912138; bh=GoKQHhtGYaNEBgykVI2GJ9mviJjgwhNyk86BEwxqxSw=; b=Q iLTOAWCNPybK5FAkHzIw9DqG2wWv3lUi66fYMJWnKht6I+ItBhpJzzgfijTMNtS4 yWeF3c8WfoQg55ghBx/FgjYFOKpLCldTOHuQ+M0PJKXIMXgrd05j4I1OsAgzZwSX VuyG0CilkfEdxT1b160VBeqFSoCUuftB9uj1Kj2SoWfRW9AUmFOlHpTbdS1NH1wd kUGdjBTw9IDwm+xc45qqBNkb1m3vPRRNKZV2kmrOiUjQr8f4RQhcAjoZkD0iBHqD TijVdyxdEutSTXGaKB5TLJWxQghvxRK51NdtSPdxz/uHT4aCqO6c4bXU10PnYVtO qxoWVyRr9CLq2R5lflKoQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvddukedgvdehucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggvpdfu rfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnh htshculddquddttddmnecujfgurhepofggfffhvffkjghfufgtgfesthejredtredttden ucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfih gvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpeevieffudffffejleeitddu hfejjefhgfegjeekgffhjeevheevkeevgedvgffghfenucffohhmrghinhepphihthhhoh hnrdhorhhgpdhmihgtrhhoshhofhhtrdgtohhmnecuvehluhhsthgvrhfuihiivgeptden ucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrd gtohhmpdhnsggprhgtphhtthhopedupdhmohguvgepshhmthhpohhuthdprhgtphhtthho pehinhhtvghrnhgrlhhssehlihhsthhsrdhphhhprdhnvght X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 8F1CF29C006F; Tue, 1 Oct 2024 19:35:38 -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 18:34:52 -0500 To: "php internals" Message-ID: In-Reply-To: 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: larry@garfieldtech.com ("Larry Garfield") On Tue, Oct 1, 2024, at 2:06 PM, Rowan Tommins [IMSoP] wrote: > 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] I would support having Python-esque context managers in PHP, and would be happy to help make it happen (if research and English writing would be useful to whoever is doing the implementation). --Larry Garfield