Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122612 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 81D721ADA75 for ; Mon, 11 Mar 2024 16:14:11 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1710173666; bh=Ne7C2TSqEf+CKfZpwG7r1Z4ZGce1Qv5ISuDoRGSTkGM=; h=References:In-Reply-To:From:Date:Subject:Cc:From; b=JE/rkyXADSPtmeRWER0HkzGpkPnbmzVLlihTRJBPpUGZScOGAXHLr3s5mOoY47JQL /YtrR931DIDq+yniZ3Ggb7RdK+7LaZNK0QKU5Qd8z7y8DENI8KXLgf8roq5w36DUqf Hiu9HZ2S18Oq2FWu+sAT4rzW8ISfYfSNxLHQwMTCNkcXR+ZElOL/Enb4AJZpxYmRbO cygkIx/NS0SV/ivgKbg/2kNGpzgC+zsoTic9jtLElXv25iQ07eIdXOpH6QoVLmRL2L KfLQK/Al+m3TR67j8hvyyeKWNry6urBc5elQ04NjXqP7xNQ+VoKqQjZYHpLfHcV4lP W7vsPU7IOdc/g== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 432DA1801E5 for ; Mon, 11 Mar 2024 16:14:24 +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=1.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, MISSING_HEADERS,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-qv1-f42.google.com (mail-qv1-f42.google.com [209.85.219.42]) (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 ; Mon, 11 Mar 2024 16:14:21 +0000 (UTC) Received: by mail-qv1-f42.google.com with SMTP id 6a1803df08f44-690ae5ce241so20576316d6.2 for ; Mon, 11 Mar 2024 09:14:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1710173645; x=1710778445; darn=lists.php.net; h=content-transfer-encoding:cc:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=cMe+VIYD3uHTpSzZ8YFm9eFnlTw8eIMR5whu2aK+nMk=; b=bGdyQu5rc2BwRhCRndxceC7FBmoTy0n5t7/yJZZAGMdJl9JDqFjTtTBzGOf7KArmKD Mxreb2Oyxe6IpL7GMmeAtLQTXTa0ESd/E1K73bAH5pz/U8nhE5qa18qWTrSqughe8PRZ B56TF28RXmqBYyjlXZH8GU7jIY57JIxM2pBk4hOxCJIbk6LPeEf5A4xI5XnqLzmBGC+i eeLaKf0KcbofMQ0Vlk2VeY5KVKsfONvBzGMO+nwQJEBFCGfsTqfMLDerPHgvPMLKG/w9 baU5/aF+l1Su6HoIaCz6UE/j6CGRDZ3LfQ7Q6j8UpiW5MBMciz+0yaEOCGY/UnPYG/5B vOtw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710173645; x=1710778445; h=content-transfer-encoding:cc:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=cMe+VIYD3uHTpSzZ8YFm9eFnlTw8eIMR5whu2aK+nMk=; b=gU+be09j+DD4+ddTgOLDqFxwA2RKj8MlRywpV6+VdY9YECPyc5Sw2BCZqV/ZJOkQIb xKRzyj8nhgGhlSsSCJW5nJKyQQZqb40hw7vIP0O2e91blQiTBa03/OLFJyx4qvNYQ/1L GFX102CIAZivESW70MKAxCAMJEN3QO5W9KQtqV1+oZ2Mw54Zt54CdMlPPm+z7dZy71OR SJmUcsko1Ex3arnYxubUP0UrI5wSJLfyIoLPl8os/XajxlAi9wPO9+lqTpHEBhej0R+S Iuygf2yccVcuL/6DQmAcM+IIpR17vq2VymTTOsgDjI24veX2Cql+qb6K7x16iiHcJERF tD6g== X-Gm-Message-State: AOJu0Yxw98Kmblt+m1EJxkzB1seheqlHTmDFOFkoGVcZZ5pRp3ss4d+n waQGY0KeG64LUy+HNNoKbDKDUmqfFmAaBufVPofPxW/CsIVeebAq0eddXUvdenivTKL7YhcHYVe 1gd4H5Ax4pX4XrJWf/mhzx5zgFrkWRT+jsaE= X-Google-Smtp-Source: AGHT+IE5A55cgLbRl8VHdZoZgWhbNBRP5jyh5DjnStZ7e6tlD81GIZH6YDV1cOnNVo5qgNpcYpX8s9VYyiM0Vnt1qLI= X-Received: by 2002:a0c:ca8b:0:b0:690:c4ca:d411 with SMTP id a11-20020a0cca8b000000b00690c4cad411mr7316250qvk.59.1710173644638; Mon, 11 Mar 2024 09:14:04 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <790b5b4e-f51b-4050-a12a-5fa903d0568f@app.fastmail.com> In-Reply-To: Date: Mon, 11 Mar 2024 17:13:37 +0100 Message-ID: Subject: Re: [PHP-DEV] [RFC[ Property accessor hooks, take 2 Cc: php internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: weirdan@gmail.com (Bruce Weirdan) On Tue, Feb 27, 2024 at 6:22=E2=80=AFPM Bruce Weirdan w= rote: > > 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 wouldn'= t really work. To be clear, Ilija and I spent 100+ hours doing research an= d design before we started implementation (back in mid-late 2022). We did = seriously consider the JS-style syntax, but in the end we found it created = more problems than it solves. For the type of language PHP is (explicit ty= ped properties), doing it on the property itself is a much 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/getter d= efined > > public function __construct(private string $first, private string $la= st) {} > > 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 degre= e: > > > 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 that the de= veloper 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 vis= ibility 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 proper= ty 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 could be = anywhere in the class, potentially hundreds of lines away. That means 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 type/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-styl= e_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. --=20 Best regards, Bruce Weirdan mailto:weirdan@gmai= l.com