Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126936 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 564341A00BC for ; Tue, 25 Mar 2025 18:51:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1742928540; bh=lM6pzxvCMAsbeX3i559GD8mMofzF01MKC8amCQH6qn4=; h=Date:From:To:In-Reply-To:References:Subject:From; b=hRgjGowS+ic1Bn5r0R26QrcTqMF6e2p95u8IWXsPCly1r5mMY6Q83kqBeDkVh/Unk LLr2WjcIeeaa0QuamsKbtnE+Nst4u6BkvWfDOBX2Kov49eFwiWKbL88euyjJEMDWBy IwYiDgIFJeqSlNj0aTUQ72SH1I8Ld54mL0jpBrJI8KsGWY2eYbZeiPw8WDs6+mjDag Vga36ENrX7ADDLbfL0P9GxL01t7RHTw6quVb8YDacq2eYcK6Oll/0mhyDVgihhlEyo 6lg94PUGsTEMvDERtD9erXWeygrlgeI76tHrmV7tDAt3T47Q8W1rFyo4wxmg3yFkJt Hf0fkcvwGSIqw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 062941805D5 for ; Tue, 25 Mar 2025 18:49:00 +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=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fout-b4-smtp.messagingengine.com (fout-b4-smtp.messagingengine.com [202.12.124.147]) (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, 25 Mar 2025 18:48:59 +0000 (UTC) Received: from phl-compute-04.internal (phl-compute-04.phl.internal [10.202.2.44]) by mailfout.stl.internal (Postfix) with ESMTP id A127111400E3 for ; Tue, 25 Mar 2025 14:51:28 -0400 (EDT) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-04.internal (MEProxy); Tue, 25 Mar 2025 14:51:28 -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=1742928688; x=1743015088; bh=QjQQbp8CG96jfw/xwmiZK erGwWlTWdaSGchA16cBCgY=; b=WLYtK2/COmVbPu3JmMSEme5EcMj4bDockWij0 ITsdSdsdyPPRe7CpUtTFvbGkDdDo9KJxbiPT+czNe6jNGM9hOI+hP1JA+88x0v+f HjqyP80NDVCUYEwHgYk0Nh3aCoWKEJd8n1QccOwYQ5dpl9W2v6EAA8FzlyzbKQF/ tlwgiyq3Nq2VqyX5Pz+QQl1aWziQJeJlTil5zXE/ATSISh6SVbS/a1VikzCQhO+q mEa7B3QjAEXG9xdWYTJq6whGenfwNN5DfOfqRLEFyKbxnQ3s4hquzMi6rWJsrgvG EcfrvEjAZEz9ajrJpS8WIsHnoxyGAxh0sBEOq6HJGHRX4FJcg== 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-sender :x-me-sender:x-sasl-enc; s=fm2; t=1742928688; x=1743015088; bh=Q jQQbp8CG96jfw/xwmiZKerGwWlTWdaSGchA16cBCgY=; b=ZCAj2uPaUFYRJjp1F uW0/6C25tpiGcX4xRBtXuiGq0IwdX1EhwSlimMsWOQaPw4Ovnxc8L+arxLrMeSB4 bkz82/wEXdTwihRjNK4V0KEYXn4fWPFO+64dctcTh0TDtwESQENOEOxZBTnuyy65 hdjHgV51v98n5l9J7ihq7foDNq9rZP4z7JtKXwhCqTI5P55re899d/HaJ8p1ZicP vmb2wKOGSDWrKZRmLn/eVgjvG3F9zmIvdk545CbCH3SDgfHT0kOqInsFm4zfcgfz I8hMQLsMKNBQ8UlqUfec97sLhSwH9+L9iuDEy2aMtPib0EnP3Lr0rd+RYR2FxxKh nyFPw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdduieefgeduucetufdoteggodetrf dotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgfnhhsuhgsshgtrhhisggv pdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttdenucesvcftvggtihhpih gvnhhtshculddquddttddmnecujfgurhepofggfffhvffkjghfufgtgfesthejredtredt tdenucfhrhhomhepfdfnrghrrhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrh hfihgvlhguthgvtghhrdgtohhmqeenucggtffrrghtthgvrhhnpefgteffueduudeiuddv ffeiuddtudehfeegjefhheeliefhieelvdegjeeggfehheenucffohhmrghinhepghhith hhuhgsrdgtohhmpdhsphgvtgdqsghrrghinhhsthhorhhmrdhmugenucevlhhushhtvghr ufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivg hlughtvggthhdrtghomhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhht pdhrtghpthhtohepihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 0F5CB29C0075; Tue, 25 Mar 2025 14:51:28 -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 X-ThreadId: Td94a63d0143dd548 Date: Tue, 25 Mar 2025 13:51:07 -0500 To: "php internals" Message-ID: <09a82882-f1ee-4bdb-8a27-e46144a711f1@app.fastmail.com> In-Reply-To: <52d84a5b-09d3-4e42-9620-a62fb239c21e@app.fastmail.com> References: <3e4ba7ea-a154-452d-abfc-05ef1322fade@app.fastmail.com> <782d76d9-711a-4cee-ae0e-fe0d69973f53@app.fastmail.com> <48dce917-d147-456b-9f03-c7e23411adff@app.fastmail.com> <8a16b81c-7dab-4523-a352-76ba0cb4e771@app.fastmail.com> <9c4ac301-dfb2-49da-90e5-37a2824fc4e3@app.fastmail.com> <5b1e6d70-a1c9-455c-93d3-6b22cf1fef11@app.fastmail.com> <52d84a5b-09d3-4e42-9620-a62fb239c21e@app.fastmail.com> Subject: Re: [PHP-DEV] RFC: short and inner classes Content-Type: text/plain Content-Transfer-Encoding: 7bit From: larry@garfieldtech.com ("Larry Garfield") On Tue, Mar 25, 2025, at 6:59 AM, Rob Landers wrote: >> When I say module scope, I'm referring to something along the lines that Arnaud and I were exploring a while back. tldr, "cluster of files with a common namespace root, which can get loaded together." It was mostly about performance, but did offer module-private as well, with some nice potential. At the moment it's stalled out on "there's nasty hard edge cases and we're not sure if it's worth it" concerns. >> >> Concept brain dump here: https://github.com/Crell/php-rfcs/blob/master/modules/spec-brainstorm.md >> Code exploration from Arnaud here: https://github.com/arnaud-lb/php-src/pull/10 >> >> Still well short of RFC state, of course, but provided for context. > > My email has been broken for a few days, so sorry for the late response... No worries, I'm about to leave town myself. :-) >> In this case, I am not seeing what the nesting gets you. Making Destination a normal class doesn't hurt anything here, does it? > > I wasn't intending to write a definitive example, just to illustrate > it. A better example might be a lazy cache, using hooks to notify the > outer class it has possibly been updated: > > (note this is using the new syntax, but this syntax is NOT currently > reflected in the RFC text, yet) > > namespace Caching; > > class Cache { > private array $items = []; > private array $dirty = []; > > public function getItem(string $key): CacheItem { > return $this->items[$key] ?? ($this->items[$key] = new > CacheItem($this, $key, null)); > } > > public function saveChanges(): void { > foreach ($this->dirty as $key => $value) { > echo "Saving $key to persistent storage\n"; > } > $this->dirty = []; > } > > private function markDirty(string $key): void { > $this->dirty[$key] = $this->items[$key]; > } > > public class CacheItem { > public string|null $value { > get { > return $this->_value; > } > set { > $this->_value = $value; > $this->cache->markDirty($this->key); > } > } > > public function __construct( > private readonly Cache $cache, > public private(set) string $key, > private string|null $_value, > ) {} > } > } > > $cache = new Cache(); > $item1 = $cache->getItem('foo'); > $item1->value = 'bar'; > > $cache->saveChanges(); > > This outputs: > > Saving foo to persistent storage > > This provides for a simple API for outside the Cache class: getItem() > and saveChanges(); that's it. For CacheItem's, you only have the props > for the key or value, and updating the value marks the item as "dirty". > > Currently, if you want this same API, the only way to do this is by: > > 1. using reflection, > 2. using a closure and binding it to the other class's scope, > 3. providing a listener + observer (more formal version of [2]), or > 3. iterating over items while saving to find dirty items. > > Those choices are not ideal (IMHO). > > Reflection feels "hacky" as does using a bound closure. The > listener/observer pattern is probably overkill here, unless you already > have one lying around (i.e., using symfony/laravel or a related > framework), but still might require making some of the "internal" api > public. Finally, iterating over all the items to find dirty ones is > naive and inefficient. > > Hopefully this is a better illustration of what nesting provides? > > -- Rob I have a similar if less involved use case in my ordering library, though it doesn't need the look-back functionality. So the use case that nested classes would enable that isn't currently covered by "just use separate files and @internal, deal" is around the lesser class having private access to the greater class. Which... I believe fileprivate and modules would also address, albeit in a different way, yes? (Presuming fileprivate is available on properties, not just the class itself.) --Larry Garfield