Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:120225 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39341 invoked from network); 10 May 2023 13:19:15 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 10 May 2023 13:19:15 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id C2BED1804D7 for ; Wed, 10 May 2023 06:19:14 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-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,RCVD_IN_DNSWL_LOW,SPF_HELO_PASS, SPF_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS19151 66.111.4.0/24 X-Spam-Virus: No X-Envelope-From: Received: from out5-smtp.messagingengine.com (out5-smtp.messagingengine.com [66.111.4.29]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Wed, 10 May 2023 06:19:14 -0700 (PDT) Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 2E1D15C0417 for ; Wed, 10 May 2023 09:19:13 -0400 (EDT) Received: from imap50 ([10.202.2.100]) by compute4.internal (MEProxy); Wed, 10 May 2023 09:19:13 -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:sender:subject:subject:to:to; s=fm3; t=1683724753; x= 1683811153; bh=4283OlxBFe1qZ3Vuifed/C72mSpO4KkM6vSUIsgticE=; b=C G3I7JHW/kxYMMsSqz2nXS8n0eh2yl10jOLTpidlZbj/MfdCKDKDIDjQ/LwLD8K3n c1jnH4ZvI0xjxWMjMxkHf+kgyJ4GD22B/ygB6bVNt7wTU3//TMG05iLfqJCRuQms dmTYvy2jw6PKkbL86nxrRl2wFPB/AZ2Amor/g9fIERcPnkzZnWUaRl88UlxrvKEn JMdWXtnof1EIJyj/nL+1tncgSROtA4A7x85in07miXZW0AXP3/Kx5B/yAYq9WRVD +rL0YAnnpwwlFy8LzBrSivcBqILcCabmq7ikRF4Bu9PN6XbmtgtN+VtiVTQ3uDRm /KKBJAdyO+mpMB6MUbUGQ== 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:sender:subject :subject:to:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; t=1683724753; x=1683811153; bh=4283OlxBFe1qZ 3Vuifed/C72mSpO4KkM6vSUIsgticE=; b=J3nxzY/AH+NdHu4CNqv2+lRvT9nPd VnJuQLfhclHHtX3GzQpHvJxrdEUNM9Dt7Bhg56vF2tEJy39evgaBBWeDjdrPY4jv VKTAUip0h12w0FfcS0V73rIwyCOtXNTL23Bu4f7aSlCG57TOPXHyAY32LHUZhzdH SJf0+X1ZGl6kPnpe7mHLGctpp2LDEvGaPjQArLO9g3WtWnPNG2M25S7eL5XMghKq /KK/GtntyBkpXbHF2Sm/FsY/ZtBhJFEU/pSkNye0zzIevWMGdpdTBSZYyEuYk5ZF nSwgFigxJjdWc2vbYow0gKMVEyoMpx7Sxm9+LTyTd3UOv3O20irZnWoSA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrfeegiedggedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepofgfggfkjghffffhvffutgesthdtredtreertdenucfhrhhomhepfdfnrghr rhihucfirghrfhhivghlugdfuceolhgrrhhrhiesghgrrhhfihgvlhguthgvtghhrdgtoh hmqeenucggtffrrghtthgvrhhnpeeglefgkeduiedvvdetffeujefftdfhjeeiveehgfff keduveektddvledvvdfffeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmh grihhlfhhrohhmpehlrghrrhihsehgrghrfhhivghlughtvggthhdrtghomh X-ME-Proxy: Feedback-ID: i8414410d:Fastmail Received: by mailuser.nyi.internal (Postfix, from userid 501) id C263F1700167; Wed, 10 May 2023 09:19:12 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface User-Agent: Cyrus-JMAP/3.9.0-alpha0-415-gf2b17fe6c3-fm-20230503.001-gf2b17fe6 Mime-Version: 1.0 Message-ID: In-Reply-To: References: <641b1ca0-d33f-4f38-ae64-81b4abce24da@app.fastmail.com> <57aed5a4-f5e5-4a47-bf7a-69249f1d0ef1@app.fastmail.com> Date: Wed, 10 May 2023 13:18:50 +0000 To: "php internals" Content-Type: text/plain Subject: Re: [PHP-DEV] [RFC] Property hooks, nee accessors From: larry@garfieldtech.com ("Larry Garfield") On Wed, May 10, 2023, at 11:35 AM, Robert Landers wrote: >> Regarding $field vs. $this->propName, there's a few reasons we went that route. >> >> 1. It's shorter and less typing for what will be a common pattern. >> 2. That makes it consistent between hook implementations. In working on examples, I found many cases where I was adding basically the same line to multiple hooks on the same class. Making the code easier to copy-paste seems like a win. >> 3. It also will be helpful if hook packages are added in the future, as they'll need a more "generic" way to access the backing property. (See Future Scope.) Eg, "$field = someLogic($value)" applied to a dozen different properties; it wouldn't work if it was "$this->specificProperty = someLogic($value)". >> 4. We're used to our eyes glossing over "$this->propName", as it's so common. Having a separate name to mentally scan for to determine if a property is virtual or not seems like it will be helpful in practice. >> 5. There's precedent for it: Kotlin has almost the same functionality as we describe here, and uses a `field` variable in the exact same way. >> >> So it's mainly an ergonomics argument rather than a technical one. "Compile time macro" means it translates to the same AST as if you'd used $this->propName. There's precedent for that. Constructor Property Promotion works basically the same way. > > With using a common name for say, $value, open the door for a library > of common hook implementations (eventually)? > > Maybe something like: > > class User { > // snip > public string $fullName { get => FancyLibrary\fullName(...) } > // snip > } > > I could imagine something like this would be a huge boon to PHP if it > automatically bound the closure. $field is only available inside the hook itself, not inside functions called from it. But that does mean you could do this instead: class User { // snip public string $fullName { get => FancyLibrary\fullName($field) } // snip } Which, yes, should work fine. If such a library actually became popular, that would be an argument to implement the "property hook packages" or "property traits" concept that Swift has, as noted in Future Scope. --Larry Garfield