Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129463 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 962621A00BC for ; Thu, 27 Nov 2025 08:34:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1764232473; bh=uNaXCWIL11yz1ze0/iKAu+NCCnSh62i8QLjmADVR+OU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=dZMhY9tpj0bHiVSeiatIsTihWaun3Wqw5ZKAhabugjny6IdJz+CsBrjBU2Q+qyS7v jDEc58X6l8b4QfNA7SPkj38xVQuB+vk0w3xO1O1Zo34D1nUolo1IAPuFMNEob9ty2T zKqiItLpablVMBn6VuI0spZ+0u2Q4PVti5NmwF4ehskdqTNm0zDBZm5V/KmPfmlXWx as+hE6jPBPA1CiC836clGE6DfVQ1kNGXkYV5iFwu+I54LvHwO4r+77O21YS315zEQV 2AIfdfB1lXB8R921/CVQFDYtdzDlGHJtP8Iek1egE49whZliJghzuN+7vIzHtMDG9a KTlHFPh+CDQmg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 1F9AC180041 for ; Thu, 27 Nov 2025 08:34:29 +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, T_SPF_TEMPERROR 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 ; Thu, 27 Nov 2025 08:34:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1764232462; bh=MQbnRg+euqN6fsjwAQrXCMpMjDvJ+geRXrgPeNMUi+U=; h=MIME-Version:Date:From:To:Cc:Subject:In-Reply-To:References: Message-ID:Content-Type:from:to:cc:subject:message-id; b=VBcY3Q/PK6YGwXK8vqFvS15dULDZhjfEMsZBg9ugN3itZLEUclnOkOYBznNrrrM9C iVKIwsxNjPqFw1GCGeLDS30eSqHLv5aQnEkTrwmduqkzOdlKek7fkIBHUNPhF4Bksv DTSFIxetHFsE8L9Yjh9YGBpf74vrevJu6ECC3B5Jy7hba5Pak+tzvljvdxVEnAmWbE +3Jr8YRLsgo13IT0MaxXBASgX0RjBYyCYq0am5aZZ7IkCUIYyPCcN9sZ8Iz/+3xNYj kHVKXvjzFoosybK0ZJ/FNI0i9wt02yzc3oB000jdXYdmpWI2VAEQWrLi4cVp+oGJOR O0YBN+8dttosQ== Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Thu, 27 Nov 2025 09:34:21 +0100 To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Subject: Re: [PHP-DEV] Examples comparing Block Scoped RAII and Context Managers In-Reply-To: <26a2f13c-f318-4d6c-9595-bfaaebcbabcb@rwec.co.uk> References: <26a2f13c-f318-4d6c-9595-bfaaebcbabcb@rwec.co.uk> Message-ID: <78bfa50ad7c5111a1c6caaff3a525255@bastelstu.be> 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 I've had the opportunity to take a look now. Am 2025-11-16 00:11, schrieb Rowan Tommins [IMSoP]: > - The syntax for the two proposals is based on the current RFC text. If > they are updated, e.g. to use different keywords, I will update the > examples. The block scoping RFC has been updated to `let()`. > - There are many scenarios which could be included, and many ways each > example could be written. I have chosen scenarios to illustrate certain > strengths and weaknesses, but tried to fairly represent a "good" use of > each feature. However, I welcome feedback about unintentional bias in > my choices. 1. For db-transaction/implementation/1-raii-object.php I'd like to note that it is not necessary to proxy execute() through the transaction object. It could also be used as a simple guard object which only purpose is to be constructed and destructed. let ($txn = $db->begin()) { $db->execute('…'); } or possibly: let ( $db = connect(), $txn = $db->begin(), ) { … } In that way it is similar to the 2x-context-manager-anon-class.php example. The same “guard object” possibility also exists for the other examples as far as I can tell. 2. The RAII object in 'file-handle' serves no purpose. PHP will already call `fclose()` when the resource itself goes out of scope, so this is existing behavior with extra steps. The same is true for the 0-linear-code example in file-object. You don't need the `fclose()` there. 3. The docblock in locked-pdf/application/2-context-manager.php is incorrectly copy and pasted. > With that out of the way, here are my own initial thoughts from working > through the examples: > > - RAII + block scope is most convenient when protecting an existing > object which can be edited or extended. > > - When protecting a final object, or a native resource, RAII is harder > to implement. In these cases, the separation of Context Manager from > managed value is powerful. > > - Context Managers are very concise for safely setting and resetting > global state. RAII can achieve this, but feels less natural. > > - An "inversion of control" approach (passing in a callback with the > body of the protected block) requires capturing all variables *not* > scoped to the block. Even with automatic by-value capture, those needed > *after* the block would need to be listed for capture by reference. > > - Building a Context Manager from a Generator can lead to very readable > code in some cases, and closely mimics an "inversion of control" > approach without the same variable capture problems. > > > I would be interested in other people's thoughts. I was about to comment that the 'file' examples were not equivalent, because the context manager and IOC ones didn't include the error logging, until I noticed it was hidden away in the implementation. This probably suggests that I implicitly expected to see all relevant control flow. Exception handling and logging in particular probably greatly depend on the surrounding context to be useful (e.g. to adapt the log message or to enhance it with additional context data). So while it might superficially look cleaner / simpler, I feel that this kind of generic handling will bite you sooner or later. So with the context manager example, I would expect the “exception introspection” capability to be used to properly tear down the context, but not for cross-cutting concerns such as logging. I'm not sure if this would qualify as “unintentional bias”, but it's certainly something that affected by perception of the code. Best regards Tim Düsterhus