Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129464 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 926901A00BC for ; Thu, 27 Nov 2025 09:08:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1764234502; bh=6WNH8PEAZckY1979Gm5WmRTH0D3FmMF5Ztf7X2Feuz8=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=WkzY5WpPFBFKzkhaYuS1YZuf04bWP+XUJhB5sBQF8534n1E+waUceuZzrJxNKfxJH SAe2b7h3HRgVWDLbwnr8ewZ7Gm9zyS+JG3/KL/lH1GcWVU2uyNxpG1oksn99clisA5 0zfxlEqCg+gsayPXTIpkIHK5IncD1eNVhTuamwuIKRlHfOEqmZ/wrd9XjKQvU9tvq+ T31ka9DHFVfabhmuz6pHHQZ8a/igdA4bYXub6yZc7QcHsjMKrkGwvUx668ihIF7Xe/ 1UkL7HWKXyMJsZ+M5nNF8OPfevTPYNqaJKUScOg48a4POURRN41ti4iqsKyoYnhTQf m5DXeBDnJ3PqQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8384D18003F for ; Thu, 27 Nov 2025 09:08:21 +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.2 required=5.0 tests=BAYES_40,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 ; Thu, 27 Nov 2025 09:08:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1764234495; bh=MPiRuTMHdbGws/j3w2pyBckiTPjqV0qxeo+sfpittXk=; h=MIME-Version:Date:From:To:Cc:Subject:In-Reply-To:References: Message-ID:Content-Type:from:to:cc:subject:message-id; b=hlS66pxalid/mMPcEMP6gmiRmWp1tqmGHUu54G6IYcDT6T7IXYOiGRmtlREdWfIHu MsJ16p7ESwvS7cK1xyA6qgvgbOADyTFx3pLVYqU4AexYiWsvlg8Ice9TbF8uOGPYBy yOxZq/ggKG1CeAofeZ6YqArDvmWez3333SYP1TS7Yg9JGqqQ0NHL8B6rZwwFmB5iby 1vj/JHbgwJnWLU0z1mcQs/QUmcu7JKM0gmUItx482sPT9vEEYrLBTmaPpKMArGvc+h W71YD5mecfgnHwiVtB07R18IZmRmjECBc+ichwMZrnSGOd7klheNH5l7xIydbrdBlW hNEwOq9qJAeCw== Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Thu, 27 Nov 2025 10:08:14 +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: References: <26a2f13c-f318-4d6c-9595-bfaaebcbabcb@rwec.co.uk> <432ca4ad-7bcc-43bd-8e05-3121839b4ff7@app.fastmail.com> Message-ID: <6ca9d7e54c291d1ce8fd74b5d1977034@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 Am 2025-11-19 23:19, schrieb Rowan Tommins [IMSoP]: > On 18/11/2025 17:23, Larry Garfield wrote: >> One thing I definitely do not like is the need for a `FileWrapper` >> class in the RAII file-handle example. That seems like an unnecessary >> level of abstraction just to squeeze the `fclose()` value onto the >> file handle. The fully-separated Context Manager seems a more >> flexible approach. > > > Yes, exploring how exactly that flexibility could be used was part of > my motivation for the examples I picked. > > The downside is that it is slightly harder to understand at first > glance: someone reading "using (file_for_write('file.txt') as $fh)" > might well assume that $fh is the value returned from > "file_for_write('file.txt')", rather than the value returned from > "file_for_write('file.txt')->enterContext()". > > What made sense to me was comparing to an Iterator that only goes > around once - in "foreach (files_to_write_to() as $fh)", the > "files_to_write_to()" call doesn't return $fh either, > "files_to_write_to()->current()" does. For me the relevant keyword that indicates that the value is not used directly is not the 'as', but the 'each' part of the 'foreach'. Just by reading it as a English sentence, it becomes clear to me what is happening. The same is not true for me for `using (file_for_write('file.txt') as $fh)` or even worse `using (new Manager() as $notActuallyTheManager)` (which is part of the RFC). AFAICT the latter is not so much a problem in Python, because there is no difference between constructors and factory functions and also because there is no actual type declaration. This means `open()` could be a function returning a file handle or it could be the “constructor“ for a context manager (that then returns the file handle as part of entering the context) and the difference is effectively indistinguishable from the outside, which is not the case in PHP. Best regards Tim Düsterhus