Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:126508 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 18F2D1A00BC for ; Wed, 26 Feb 2025 05:00:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1740545899; bh=BHGQriiPLcONXjHmRoSpwESK76whT0HoYAHhDhGov7g=; h=Date:From:To:In-Reply-To:References:Subject:From; b=FeDk4koLtMBr8UAwbT4aVxSvf7APHvMGfijDBrxTw6lANEVtgk5FkEuyYa3jbJee2 wocW5jLXvzyPcR339tVwgE5VVWY8BMDKfIi7Tx6HHLjVIPrd+zO0OGrX+IbDi/V7ca SPUGwzNQ8r52vkj8Uybcjew2qLkaCIVVNKsGwsNfkaGTSfCsEJMZJe/TMJqxDFU0mF gWFaJssIL/EbXhwQCWEQ848AW+MuVoq/khHvcnvDwsZZ0BUEhKNbWW4Ysdxmf3OP31 d1esr1hG4HSgF7tEjbJy4YtUd/Y9cn9wsMG1t445iAkKf801yB6MW8nyN4dcUi4Qcy Csnva3RrXon0Q== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C7AE318006B for ; Wed, 26 Feb 2025 04:58:17 +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, SPF_HELO_PASS,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from fhigh-a7-smtp.messagingengine.com (fhigh-a7-smtp.messagingengine.com [103.168.172.158]) (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 ; Wed, 26 Feb 2025 04:58:17 +0000 (UTC) Received: from phl-compute-10.internal (phl-compute-10.phl.internal [10.202.2.50]) by mailfhigh.phl.internal (Postfix) with ESMTP id 428CF1140189 for ; Wed, 26 Feb 2025 00:00:55 -0500 (EST) Received: from phl-imap-06 ([10.202.2.83]) by phl-compute-10.internal (MEProxy); Wed, 26 Feb 2025 00:00:55 -0500 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=fm1; t=1740546055; x=1740632455; bh=U30DZil0KJiJhgl7BDGJi ZtSELJUOEO8C7oNo1b1l2w=; b=a3IYUohhWlgc1KT+iNJZeXlPC62+kyrrQkCmw wQog9p+Q3/ay/b7sm2n05/aNW3le+dtZc/6LQcm9T2VDWVrxhJV8H2vgGa5w42Ts Oib0583wEl/wczV2059GfBa2EPIQWKpqcwdQT3myO51ltiAvGFFbWi5QgInbAvVt 4wbPZNNu+p+ZE9jSfzt/s4uIX+Thkt5KtP6dC7hZ+V2BVNo5EDjLtnLQ97BLLD/w wth4X8i8+o4sWybkT5ja4dY5psH6HUpeccacW17KL3s89P5hqetNk/vCTChC+c/c DFeNA7AJZeDKIxGWSII6mcf0nH5E4KMzN+89lFFWS+QSs+9tg== 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=fm1; t=1740546055; x=1740632455; bh=U 30DZil0KJiJhgl7BDGJiZtSELJUOEO8C7oNo1b1l2w=; b=J85GvJGk5BDpLEqhV D9BUqMsuV7mhg8OVHecorMftjbPiBs3EqMN6YbNlfWRF4PFLA07F8fvqGMl9kWBQ Hafn9WtA2dZknVKXC2XtdjlvVSYiBt9xYI+4bHGBTHyE+hwgoVFhxPRd4PGC/AvS 6fsXF1+SZOcSM+XGP1rIIBsCe6fOpjhUTWqxohQurCkIlvnFbAuPhJkYGX0BFSTg 87gJ9JWZRLLw5pWGs0DUKxmYWaty15pWL7Si+SfcHJ1G8Io2Wtnjs6vpZiVpBDte YZafjK/96SjgoNz01CVl5QOqR7jbmBXQ0INz35mcwOTP/Q6wx4JGdh5RxYOMoTWF GJHMA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeefvddrtddtgdekfeeijecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivg hnthhsucdlqddutddtmdenucfjughrpefoggffhffvkfgjfhfutgfgsehtjeertdertddt necuhfhrohhmpedfnfgrrhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfh hivghlughtvggthhdrtghomheqnecuggftrfgrthhtvghrnhepheelkeelvdeuheffkedv heekueelgeevheffgfelfeejudetvdetieeigfffgeeunecuffhomhgrihhnpehgihhthh husgdrtghomhdpvgigrghmphhlvghsrdhmugenucevlhhushhtvghrufhiiigvpedtnecu rfgrrhgrmhepmhgrihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homhdpnhgspghrtghpthhtohepuddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohep ihhnthgvrhhnrghlsheslhhishhtshdrphhhphdrnhgvth X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id C805D29C006F; Wed, 26 Feb 2025 00:00:54 -0500 (EST) 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, 25 Feb 2025 23:00:33 -0600 To: "php internals" Message-ID: <62b24fbc-d596-4ade-bf82-4861287ca1ae@app.fastmail.com> In-Reply-To: References: <0b408765-04c4-49ed-bd5a-bceb34a2a3f1@app.fastmail.com> Subject: Re: [PHP-DEV] Consensus gathering: allowing unsetting of backed property hooks Content-Type: text/plain Content-Transfer-Encoding: 7bit From: larry@garfieldtech.com ("Larry Garfield") On Tue, Feb 25, 2025, at 5:31 PM, tight.fork3192@fastmail.com wrote: > On 2/25/25 4:51 PM, Rowan Tommins [IMSoP] wrote: >> I actually started writing an RFC to rationalise some of this behaviour > > I'm glad I'm not the only one who considers this an issue worth pursuing! > >> Here are some of the things that might happen as a result of unset($foo->bar): > > I don't disagree that there's a lot of weirdness. But for better or > worse that's where PHP is now - it's a fundamentally weird language. I > think it's better to be consistently weird than to be inconsistently > weird. It would be inconsistent to allow unsetting some types of > properties but not others, and which ones can and can't be unset are > indistinguishable to a 3rd-party consumer. That's a footgun - the fact > that it's caused by an evolution of weirdness is neither here nor there. > >> If we allow a hooked property to directly "unset" the backing value, what *exactly* will it do? > > I can't comment from an implementation perspective, but as a user of > PHP I would expect unsetting a backed property to return the property > the "uninitialized" state, and subsequent access would proceed as if it > were the first access of the uninitialized property. > > Unsetting a virtual property could simply do nothing, but not result in > a fatal error. I don't think a warning is even necessary because no > action is taken. > >> If we add an "unset" hook, what's the default behaviour if one isn't defined? > > Adding an unset hook could be out of scope of this proposal, but if > there's sentiment that one should be added, not defining one would > result in the behavior described above. I certainly don't want to be > required to define an unset hook for every single backed property; > rather `unset()` should have a default behavior. > >> I think it's one of those features that sounds simple when you look at a single use case, but actually specify the behaviour in all cases is going to be a lot of work. > > Definitely. I do think that hard things are worth pursuing in the > interest of consistency and functionality. But then again I'm not the > one implementing it! Most of the use cases you talk about are easily emulated from within the class. If you're calling unset($foo->bar) from outside of the class, I would argue the code is bad and needs to be rethought to begin with. From within the class, the RFC linked to several examples, the first few of which involved caching of derived properties. https://github.com/Crell/php-rfcs/blob/master/property-hooks/examples.md Most notably for now: class User { private array $cache = []; // ... public string $fullName { get => $this->cache[__PROPERTY__] ??= $this->first . " " . $this->last; } } And then internal to the class, unset($this->cache['fullName']) works perfectly. Problem solved. And as noted, if you're calling unset() on a property from outside the object, PHP isn't the problem. Bad code design is the problem. Fix that instead. --Larry Garfield