Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122516 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 A39AC1ADC91 for ; Tue, 27 Feb 2024 17:22:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1709054563; bh=sB9y0ry8tnEZ9a9W/m6+5p7jgj6sLvuhT90lAjRe2XI=; h=References:In-Reply-To:From:Date:Subject:Cc:From; b=SBsfRD6DMxlohxriSzlmjZ36PUSdFEY+V+mPgUXjpcsMsSyxgqXmWQw2JzmY+8IeX Bk8aLfrloAkCnNdbFdaoNPbNCtH+QjtsewgnsYLafN9GIoQS/K3/t0x5nLBCHr3Hxh D+JSAmFz6wzdOkE30s/GWYzdl0taSp9Fq68OFqVbscrbZLpHSlqrgHsGknB7LV9TZj T8imwLaPg7Mtno5kGhDPYkbfyAImITgHRnKGJMlr4QGiwQ8r3/QrHwuVNvl5uZZBuQ AtSxLWYPJTE0UWEbcMyaeK7SF66taytBArsXXpvBHz7TPrqjRvV5O6fnlRH5oY2bIj 6alRSGpcUeMzA== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id AD9611806C5 for ; Tue, 27 Feb 2024 17:22:41 +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.8 required=5.0 tests=BAYES_40,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: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-qv1-f44.google.com (mail-qv1-f44.google.com [209.85.219.44]) (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, 27 Feb 2024 09:22:37 -0800 (PST) Received: by mail-qv1-f44.google.com with SMTP id 6a1803df08f44-68fef04c5c1so11521016d6.0 for ; Tue, 27 Feb 2024 09:22:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709054548; x=1709659348; 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=Cd+q8jre1f8xVONh9NahB2+EkPZflddFw2lq6A0v0wk=; b=CXcDHCYIT0NZmSpd/JFzqfOycvfIFlln7BjHnAuqOlMuKkYxhFJSRgOXv7s5an+Kaa oreckl+SY8iwa8fbPVYjsog2HsXXgYH8CHUtUca1gAbhqdqYnkLI82LHP4CWg6GZSA8l +JY+XoCekUyJ7iqGF66oyox857UH/GO8kY/OTxxLXy5bIKVL4WizawsCfEjacN0ltQrk /NEOxnNB6tmdqLalaala8EVdglnr3OvuUpdztxK/dk1+5FtuLo1nmRArnSFJSY6MQ9yo 1FaZNYwRycB2LqwoCX6c0IrTsxr378dr1dBGU0tzzmpEoK4mtVUM7hUAL3o5gJ+Mm8sK pZ3A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709054548; x=1709659348; 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=Cd+q8jre1f8xVONh9NahB2+EkPZflddFw2lq6A0v0wk=; b=MGFct4duis+BS2tPwWK1xS0NLykXA7iE+1YVZHTGR3XqFxhG/CZPK3V1N8KCb5vG2a vvHp0GSc/n1paTw2WpJbwc8aRWKXD4dTGIhUAB4xuNcz8ihdzF+F943tLae0YG5BcdgO xnDN1QW0mVP+s9WmA41up4KlB4JYRorML9JbQGbF6Kqmio+B/ZIw27+jmUi1vOSrFjx3 JMEGKSnZXXoVHd9ROq0ijih+tCM56D1zdR+dvFjK0NIzujb4YpGB6cjZM3VR4mwmHKM7 7SFhFnDWqe6g9hhZM0rOk+e6aaxo2rB5dJDc/vFCr9tD3bVGJ+Sl+DUsbwH0MH9eZ6tR eu3g== X-Gm-Message-State: AOJu0YwEQJIqKG7XoeVRgPjGWP92fH0Pd96+ki1+myAJRtUAz840I+vO fRGY8uhM5iQ2brryP2kDrWJJxp1uhGbvJmkuFdm/B8YdFnV1ODmk5WoRPOTMZOO7Oynnl3NGk4b wDPmR0jL41W0wC0vHGqhGBVwNSJick05dbI0= X-Google-Smtp-Source: AGHT+IEUKwBwK2ifd6ykMmsAqYfHubTSQAWAnKoaTYbI1GK4zYjn3S2TH6lV9tXfUHcXccrOUWYaFGYqrgQ/zb+73kk= X-Received: by 2002:a05:6214:1d0b:b0:68f:2d80:c41c with SMTP id e11-20020a0562141d0b00b0068f2d80c41cmr3862156qvd.24.1709054547602; Tue, 27 Feb 2024 09:22:27 -0800 (PST) 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: <790b5b4e-f51b-4050-a12a-5fa903d0568f@app.fastmail.com> Date: Tue, 27 Feb 2024 18:22:00 +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) 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 and = design before we started implementation (back in mid-late 2022). We did se= riously consider the JS-style syntax, but in the end we found it created mo= re problems than it solves. For the type of language PHP is (explicit type= d 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 def= ined 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 degree: > 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 f= irstName()s return and set firstName()s parameter to be the same. Even if w= e could detect it as a compile error, it means one more thing that the deve= loper 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 visib= ility 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 property= 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 an= ywhere in the class, potentially hundreds of lines away. That means just lo= oking at the property declaration doesn't tell you what its logic is; the l= ogic may be on line 960, which only makes keeping its type/visibility in sy= nc 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 --=20 Best regards, Bruce Weirdan mailto:weirdan@gmai= l.com