Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122765 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 E2D511A009C for ; Tue, 26 Mar 2024 22:47:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1711493279; bh=uKi1jtJfHjD7STo38iscrZ4Gf0k8SZKODAqKw+m3HMo=; h=In-Reply-To:References:Date:From:To:Subject:From; b=mdo1HH0BEzKrcKNww2+f+r1xGj3jyKDmscbA64jF0l+U+onbJ/S28EbYHCdTLtm6W IfFHEbzaFqf53NdHVc1f4/L2MJHJjbBpLsgvEtALNQ5beTUKzfl1xcPm7wgljPpeyT GixVBi4Xwea84gPGrEBjDI1JUNVz+PRUy1Dd9rCZxkXGXEW7/iubr9MGDRS0NT9Lib 2y/rmlMqx4lLsx4llBvBEqs5vA13BglxvErusjQ6uM66IuEr+AhNovJYD41XcHgFYX kt1UD/9FkNwmblBA2sDU57YnZhmLjHFs8LDXNck1oVKtvinoJfTSM0dSUJtkMl455n Liu/kmnjLl9Rw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id D8F95180061 for ; Tue, 26 Mar 2024 22:47:57 +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=-0.1 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_NONE, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from out4-smtp.messagingengine.com (out4-smtp.messagingengine.com [66.111.4.28]) (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, 26 Mar 2024 22:47:57 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 95DF55C005A for ; Tue, 26 Mar 2024 18:47:31 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Tue, 26 Mar 2024 18:47:31 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= garfieldtech.com; h=cc: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=1711493251; x= 1711579651; bh=uSJ1CgjnwnqtX0jnWaU9LmEjs8es2jpDk39BxeqQV5s=; b=k VYR64nnuLYGY4/EHykwMVOH+OLHfp23jhxAREtyo1eu2Q0fqAr3mybobZlA5vCCy 6S5u6/VnZvZ3/SuApVv5cMfJmVDEL2C3k9K2PiVywrrlvSZtbFixg6BF2PGAyRQw iQxjUp/mlPi+2uzibJW1wQZox2LUu/iUJCavaNPWYjYmtgoK/RgdBwqeEHJfWIa2 OxZxZeC04mekCHBXYNso0QkzQpA+UMHXoLdb7XfiYzIhDrM3MEzK7G62AG9Nq0FN hJ/4vn9HfDN1NeCDJeIfN1pnzoXfGuRwq9uQG5TqF/coNk36ccuPA0r1wO3fSYFC oWwmI4igyVJquAm9BppRA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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-proxy:x-me-sender:x-me-sender:x-sasl-enc; s= fm2; t=1711493251; x=1711579651; bh=uSJ1CgjnwnqtX0jnWaU9LmEjs8es 2jpDk39BxeqQV5s=; b=i4ulTs/D4E6A23ODr+podxyBedInyHR6vC55p39gSlvu p/WyQZ2nvIuw6/MM51Hmtw7rwj/GJKxiTV9u7bCWnwgYaWWjFj0DerBR9U0Ew7Oc k6xC81qrrOtKHUFrj7voO9ZA1wV0nh+qUOwvGrx0M7e/D+8GT36ayN+IGEE2ZTZs R0GLMVzRZDI+bZ3gbh9zBQfOCgzH5x88NcYJk4LgEdPiHZJQX50t/iZdekkK4DD6 0ECgkX5f3FTGkhhRxrxSDLoa0m3P85kVP/1jL+prI06WliegRKqH42N+UtoaXJdm 5AYC6RupAYFDydw4BPiUFvqF2QbVEa7htVQeWSXtdw== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledruddugedgtdefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 3398B1700096; Tue, 26 Mar 2024 18:47:31 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-328-gc998c829b7-fm-20240325.002-gc998c829 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: In-Reply-To: References: Date: Tue, 26 Mar 2024 22:46:55 +0000 To: "php internals" Subject: Re: [PHP-DEV] [RFC[ Property accessor hooks, take 2 Content-Type: text/plain From: larry@garfieldtech.com ("Larry Garfield") On Tue, Mar 26, 2024, at 8:18 PM, Jakob Givoni wrote: > Hi, thanks for the RFC and the effort put into trying to make it > palatable to skeptical minds! > > After reading most of the discussion in this thread I believe that the > RFC in its current form can work and that I will get used to it's > "peculiarities", but an idea occurred to me that may have some > advantages, so here goes: > > Use the "set" keyword that you've already introduced to set the raw > value of a "backed" property: > > public int $name { > set { > set strtoupper($value); > } > } > Or when used in short form: > > public int $name { > set => set strtoupper($value); > } > > Advantages in no particular order: > 1. Shorter than $this->name > 2. No magic $field > 3. Short and long form works the same > > Disadvantage: "Set" can only be used to set the raw value inside the > hook method itself. Or maybe that's a good thing too. To be honest, I > don't love that $this->name sometimes goes through the hook and > sometimes not. I'd prefer if the raw value could only be accessed > inside the hooks or via a special syntax like f.ex. $this->name:raw > > If there are any use cases or technical details that I've missed that > would make this syntax unfavourable, I apologize. Interesting idea. Not being able to write the raw value except in the set hook isn't a bug, but an important feature, so that's not a downside. (Modulo reflection, which is a reasonable back-door.) However, there's a few other disadvantages that probably make it not worth it. 1. `set` is not actually a keyword at the moment. It's contextually parsed in the lexer, so it doesn't preclude using `set` as a constant or function name the way a full keyword does. (PHP has many of these context-only keywords.) Making it a keyword inside the body of the hook would do that, however. 2. Like $field, it would be a syntax you just "have to know". Most people seem to hate that idea, right or wrong. 3. Like the considered syntaxes for parent-access, it wouldn't be possible to do anything but a direct write. So `set => set++` wouldn't be possible, whereas with $this->prop all existing operations should "just work." 4. Would we then also want a `get` keyword in the get hook to be parallel? What does that even do there? It would have the same implications as point 3 in get, so we're back to $field by a different spelling. So it's an interesting concept, but the knock-on effects would lead to a lot more complications. > Another observation (I apologize for being late to the game but it was > a long RFC and thread to read through): > > What would happen if we stopped talking about virtual vs. backed > properties? Couldn't we just treat a property that was never set the > same as any other uninitialized property? > What I mean is, that if you try to access the raw value of a property > with a set hook that never sets its own raw value, you'd get either > null or Typed property [...] must not be accessed before > initialization, just like you'd expect if you're already used to modern > php. Of course you'd just write your code correctly so that that never > happens. It's already the case that uninitialized properties are > omitted when serializing the object so there would be no difference > there either. > > The advantage here would be that there's no need to detect the virtual > or backed nature of the property at compile time and the RFC would be a > lot shorter. Unfortunately the backed-vs-virtual distinction is quite important at an implementation level for a few reasons. 1. A backed property reserves memory space for that property. A virtual property does not. Making virtual properties "unused backed" properties would increase memory usage for values that would never be usable. 2. There would be no realistic way to differentiate between a get-only virtual property with no storage, and a backed property that just happens to have a get hook but no set hook. Meaning you would be able to write to an otherwise-inaccessible backing value of the property. 3. That would then appear in serialization, even though it's impossible to get to from code without using reflection. Which is just all kinds of confusing. So for practical reasons, the distinction isn't just a user-facing difference but an important engine-level distinction we cannot avoid. Cheers. --Larry Garfield