Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122616 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 DE6821AD8F6 for ; Mon, 11 Mar 2024 17:44:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1710179099; bh=mkj7fTBsRykO2KsJHLNSUrgs1gjAlbkxtqzxY4tE7U8=; h=In-Reply-To:References:Date:From:To:Subject:From; b=dtSMJjGVLQ6OTR1UPoC24gHV67E9NAdoL0Z+QmW8COKJZ8UsS1jJ3wBmpOu/ENnjL mYyVWH29yPC31bXCsiwAsbcf5H2nnR9fkQ9D+ttHu3eBwICQGVkUDpTKQ6DegZKeSf CT/hWBwTcBnh3r8Q9tKa15cjfYXLnuZ3LTOlyVA8o6rqHlwX4k6g8owaOcFuCDvqxh NXMgxE3Jf1EeLITl0pA9PV6UgqBwroF6G6S9c7Y1N0F78bEt4wFkoEhprNpyjJdKYT fE9P765m3lqAV4qyS8NWcgaNDgdTGoUARaQ/sa6mKCiLFhoHahqJy5UChYUhqaLXdQ OWhSufkeEJOVQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 8DB9D18072E for ; Mon, 11 Mar 2024 17:44:58 +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, 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 out3-smtp.messagingengine.com (out3-smtp.messagingengine.com [66.111.4.27]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 11 Mar 2024 17:44:57 +0000 (UTC) Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id DD1455C0070 for ; Mon, 11 Mar 2024 13:44:41 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute1.internal (MEProxy); Mon, 11 Mar 2024 13:44:41 -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=fm1; t=1710179081; x=1710265481; bh=KqB3MKn1jn2/N48k3rerk ytYV5khjEPwHQ2OyNtA5IU=; b=uDtPHnBvWvnNJoXhdg3EyR3HG4q653hmbXJXb 6uwemp/Ap3F5cTmKksR6b+t+UdOwXd8oY5kTXzgOOBS2ycIiMcmHUhtHNcvlWFMb OXwaKvFjmOV6kYePsnkN0foojGd925BjQW6DQi07/tv/dOFTz2fZI2cie4Lczlvs grh2i1zmvpwv+8i2cKx9xY95MEDf2+2b/4VTGNiQDus9zyM3+Dz3Dh7kh+Lbnl+F jfIByYT++mzmW9fulfvU65gKiIHbdj4jstgFn4iGHBrfK359kZcbhn7mdd+5YcAx NtCaQoJgP7QO1TCIK87VCwqkbT0MP962Q09ZakxwLw3ET8ABA== 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-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1710179081; x= 1710265481; bh=KqB3MKn1jn2/N48k3rerkytYV5khjEPwHQ2OyNtA5IU=; b=I Upt0UEND0r9HGPSYe3fxL2il9y4KXy4TEc/jB0brFMDPN7EHMUWW3Vbx5Vc97JzW Qct+Ytw9BZZfEDJUyCnSyX2NeO1M2T4nDXGKoDDKxlZVrE+flzHITKR/+dMkxxqg Hfoljsz8SdfFpWGyNgenZM3+ilfcVC9yFhvQ+GeAwC9HDxs7w3gHKRXyLLWBFMny XaBj+NsOsCyi7W6ABUsTowe1hB3kPM4lPvg/iqliKap2f7QBxT/nvq35cj5kteiW 07oTuXB3OXQ3GrcmmY8QbevdstU0ZseKB4mDzj8w1GgQTfQvWxntptbCQ4J8k4Xh T01usUJu96R5/h5SSZbgA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvledrjedugddutdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgfgsehtqhertderreejnecuhfhrohhmpedfnfgr rhhrhicuifgrrhhfihgvlhgufdcuoehlrghrrhihsehgrghrfhhivghlughtvggthhdrtg homheqnecuggftrfgrthhtvghrnhepgeeghefgteejheeggfeghfelueeggfdtjeeivedv tefhveeguedufeelhedvteeinecuffhomhgrihhnpehphhhprdhnvghtnecuvehluhhsth gvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheplhgrrhhrhiesghgrrhhf ihgvlhguthgvtghhrdgtohhm X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id 671371700093; Mon, 11 Mar 2024 13:44:41 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.11.0-alpha0-251-g8332da0bf6-fm-20240305.001-g8332da0b Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Message-ID: <9b2dc2ae-bc00-4572-821f-08eaedf20a66@app.fastmail.com> In-Reply-To: References: <790b5b4e-f51b-4050-a12a-5fa903d0568f@app.fastmail.com> Date: Mon, 11 Mar 2024 17:44:20 +0000 To: "php internals" Subject: Re: [PHP-DEV] [RFC[ Property accessor hooks, take 2 Content-Type: text/plain;charset=utf-8 Content-Transfer-Encoding: quoted-printable From: larry@garfieldtech.com ("Larry Garfield") On Mon, Mar 11, 2024, at 4:13 PM, Bruce Weirdan wrote: > On Tue, Feb 27, 2024 at 6:22=E2=80=AFPM Bruce Weirdan wrote: >> >> Hi Larry and others >> >> On Fri, Feb 23, 2024 at 12:57=E2=80=AFAM Larry Garfield wrote: >> > >> > >> > I've added an FAQ section explaining why the Python/JS approach wou= ldn't really work. To be clear, Ilija and I spent 100+ hours doing rese= arch and design before we started implementation (back in mid-late 2022)= . We did seriously consider the JS-style syntax, but in the end we foun= d it created more problems than it solves. For the type of language PHP= is (explicit typed properties), doing it on the property itself is a mu= ch cleaner approach. >> >> >> The section you added [1] seems to focus on having both `public string >> $fistName` and `public function get/set firstName():string`, and how >> it's hard to keep types and visibility in sync. But I'm not sure if >> you considered making properties and accessors mutually exclusive. I >> mean the following: >> >> ```php >> class Person >> { >> public string $firstName; // compile time error, setter/gett= er defined >> >> public function __construct(private string $first, private string= $last) {} >> >> public function get firstName(): string // compile time >> error, property defined >> { >> return $this->first . " " . $this->last; >> } >> >> public function set firstName(string $value): void // compile >> time error, property defined >> { >> [$this->first, $this->last] =3D explode(' ', $value); >> } >> } >> ``` >> >> This seems to address most of the counterpoints you listed, to some d= egree: >> >> > What is the property type of the $firstName property? >> >> Well, you propose to allow wider write-types yourself, so the question >> would apply there as well. But presumably, the property type is its >> read type - so whatever getter returns. >> >> > but there's nothing inherent that forces, public string $firstName,= get firstName()s return and set firstName()s parameter to be the same. = Even if we could detect it as a compile error, it means one more thing t= hat the developer has to keep track of and get right, in three different= places. >> >> With mutually exclusive accessors and properties it becomes just two >> places. And yes, accessor consistency would need to be checked at >> compile time. But the same can be said for the widening of write type >> you proposed. >> >> > What about visibility? Do the get/set methods need to have the same= visibility as the property? >> >> When there's no property the question becomes moot. >> >> > If not, does that become a way to do asymmetric visibility? >> >> Yes. >> >> > What about inconsistency between the method's visibility and the pr= operty visibility? How is that handled? >> >> There's no inconsistency when there's no property. Accessor visibility >> can be different - allowing the asymmetric visibility you wanted to >> implement in your other RFC. >> >> > How do you differentiate between virtual and non-virtual properties? >> >> This one is hard to answer without asking another question: why would >> you need to? Does the requirement to know it stem from engine >> implementation details, or do you need as a person writing code in >> PHP? >> >> > For non-virtual properties, if you need to triple-enter everything,= we're back to constructors pre-promotion. Plus, the accessor methods co= uld be anywhere in the class, potentially hundreds of lines away. That m= eans just looking at the property declaration doesn't tell you what its = logic is; the logic may be on line 960, which only makes keeping its typ= e/visibility in sync with the property harder. >> >> Forbidding property declaration reduces that to double. The rest is >> mostly stylistic and can be said about traditional >> (non-constructor-promoted) properties as well. >> >> Now this approach naturally has some open questions, foremost about >> inheritance. But we probably don't need to go into those details if >> you already explored this way and found some technical obstacles. If >> you did, it would probably make sense to list them in the FAQ section. >> >> [1] https://wiki.php.net/rfc/property-hooks#why_not_pythonjavascript-= style_accessor_methods > > > Resending this since I've never got a reply and it's quite possible > the message got lost due to mail list issues. What you suggest might be possible, but it still runs into the problem o= f backed properties. Plain methods work fine for virtual properties, wh= ich makes sense for Python and JS as they don't have pre-defined propert= ies. Once the language has predefined properties, a method-centric appr= oach leaves a lot more to be manual, which makes it more work to achieve= the same end. And then what if you want hooks on a property used in co= nstructor promotion? Or if you cannot declare a property and similarly = named hook-method, how do you add hooks to a plain property in a child c= lass, something generated code like ORMs will most likely want to do? Even if it could be done, what would be the advantage? Other than "it l= ooks like JS", I don't really see any benefit, just a lot of complexity = we'd have to sort out and find the edge cases on all over again, and bas= ically rewrite the whole RFC. Unless someone can show why the property-= centric approach is fundamentally broken (and given that we have a succe= ssful implementation already I think that is unlikely), there's little r= eason to revisit the fundamentals of the RFC at this point. (And, mind you, the above discussion does not demonstrate that a method-= centric approach is in fact equally feasible. At best, there are probab= ly ways to make most things work, but without actually doing it there's = no way to be sure it wouldn't run into a brick wall somewhere.) --Larry Garfield