Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:129468 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 0675C1A00BC for ; Fri, 28 Nov 2025 09:00:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1764320460; bh=0X+BBDy3FNXASwi+pquVV4Q7KcO3MNX0OirG+UVaEJU=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=DmSnucs9fJ/OctIdHeSTyP0VMS+x2axwvr3NYS0fDpDMM4fd6AFGncoHSq2vDmhRu GtWLqvGD69buwDMC3OLefkTvw3riND5kw3jFqmCZQvQcv3aiW4fnvqQw9XvqNZF/GK k9noHUcC+OTEGpFv76c3PK6LOa6vzc6wlex9SpolBZB4+xevTKFMo4OgQolOj80FFF HkGwWBLVzFc9AcEssUxEKrb76+tyiFC+FdXNUYxsOSBrXMje6fCNc75sYDJebG+mYs wCNamy8BmmYq6dhalh8hdNA9zz+iyiuaDwbaidiRoCpjqtARObG1gweaZq+aHHdZN5 ZAQF3oD7zRSzA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4C61E180072 for ; Fri, 28 Nov 2025 09:00:59 +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 ; Fri, 28 Nov 2025 09:00:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bastelstu.be; s=mail20171119; t=1764320452; bh=1+sudQJcMDOc1V+fyrQPrqbsuukglE7sJxplxTwl3zk=; h=MIME-Version:Date:From:To:Cc:Subject:In-Reply-To:References: Message-ID:Content-Type:from:to:cc:subject:message-id; b=Q2i7igo5mpLthZjTJPxG298oumt+jHKlFYl5Wu6SqtEG9QvtA6mXpOQ9Z5iXw/NRR 7RX+4pVqIAIJroom+ngM0gaANlcltSNCmdiWhYklkq32ipwfbVsVcOowsrdb5kf9Jf odN6YEA4AdKANR6ij9sSAVFy+xAvMd44KO+XeAzqQE4bkys3aou/c8kMdrXA44QgOF A70ZumAnsu2E9ZvrgRWR5Pcx3CpFNBxo4fQu8y4zCQCBkF0+UOLkP9GeBQtQlDm9X1 3APJmcVyikpVijlaVE6Q3aDX/P4Zarxkx8khH51Zxaj7+4j5Er0ueSpsw9gBXKYVem fmDD51VPWQYQQ== Precedence: list list-help: list-unsubscribe: list-post: List-Id: x-ms-reactions: disallow MIME-Version: 1.0 Date: Fri, 28 Nov 2025 10:00:52 +0100 To: "Rowan Tommins [IMSoP]" Cc: internals@lists.php.net Subject: Re: [PHP-DEV] [RFC] Context Managers In-Reply-To: <1CFE3E0B-23F5-4413-99DA-FAF308579A5A@rwec.co.uk> References: <1CFE3E0B-23F5-4413-99DA-FAF308579A5A@rwec.co.uk> Message-ID: <60e14be9c18f8e1dc8dfc5143fda5b5d@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-06 13:18, schrieb Rowan Tommins [IMSoP]: > I think the relationship to Iterators is significant: if you put an > iterator in a variable, it's perfectly possible to use it in two > different foreach statements, or manually call the interface methods, > and get very confusing results. > > But most of the time, you don't take a reference to the iterator at > all, and the same would be true of Context Managers: > > foreach ( $foo->iterate() as $item ) { ... } > with ( $foo->guard() as $resource ) { ... } The reference to the sibling thread probably fits even better here than in my previous reply. So I'm putting another reference: https://news-web.php.net/php.internals/129467 > there's nothing stopping you calling __construct or __destruct as > normal methods, and causing all sorts of unintended behaviour. That is *technically* true and there are indeed some “expert-level” use cases where calling these manually - except for `parent::` I guess - is required (e.g. lazy objects), but there's a very significant difference: Those are magic methods following the naming scheme of magic methods. The `__` prefix is a visual indicator that the methods are somehow special and probably not meant to be used directly. The documentation at https://www.php.net/manual/en/language.oop5.magic.php states (highlighting mine): > Magic methods are special methods which **override PHP's default's > action** when certain actions are performed on an object. > > All methods names **starting with __** are reserved by PHP. This is not true for the context manager methods. From a developer's PoV these are normal methods on a class with nothing screaming “be careful”. The interface *could* serve as a invitation for folks to read the documentation on how the interface methods are meant to be used, but we all know how that works in practice with an IDE suggesting normal-looking methods that appear to do the right thing (until they don't). Best regards Tim Düsterhus