Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127428 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 lists.php.net (Postfix) with ESMTPS id 18B2E1A00BC for ; Thu, 22 May 2025 15:26:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1747927452; bh=4odW1Ld6OdR9bsAUYGgMnALhjM0wNa5YUaRyyU7VeFE=; h=Date:From:To:In-Reply-To:References:Subject:From; b=OIKc31gVn7W+fUWnW5qpONf4SfeWkAMWQG7bE/Lc3iXnWUSzFHKMARTstc9QIjjy8 EX/Xvn6ELgV20q63YRaW8bTPs99cTcUlBtrftNVs4Wk0r5RQmK9dmWWjPubQIwj8wI frn5chTYiIz354amScmkU0mqVp4y0kpYiGG0INBqwNUMJ0KuZNKC0hoUGZdF0zGj3Q tsAGrJ+TmJ8XkemEMdabCEgtZxmGUvbGbRFmTdPj/1nsdKABy6WtnS9WPh4uoEwxcq IT8P4duZti044AIZ1otTAh97XeDmPa0X8GOFDSwsaMIKTPAN649QekI2ZqfQ0auXBn /9y+R7Mo2OZkw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4BA77180034 for ; Thu, 22 May 2025 15:24:11 +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=-2.8 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_MISSING,HTML_MESSAGE, RCVD_IN_DNSWL_LOW,SPF_HELO_PASS,SPF_PASS 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 fhigh-b3-smtp.messagingengine.com (fhigh-b3-smtp.messagingengine.com [202.12.124.154]) (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 ; Thu, 22 May 2025 15:24:11 +0000 (UTC) Received: from phl-compute-05.internal (phl-compute-05.phl.internal [10.202.2.45]) by mailfhigh.stl.internal (Postfix) with ESMTP id A6735254011A for ; Thu, 22 May 2025 11:26:18 -0400 (EDT) Received: from phl-imap-09 ([10.202.2.99]) by phl-compute-05.internal (MEProxy); Thu, 22 May 2025 11:26:18 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bottled.codes; 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=fm1; t=1747927578; x=1748013978; bh=jg927pSIYP TcVjkFJZDCKmRqmJTHjZTuc4rC6Ej3D70=; b=Jj6ZDKQ7lgQ5Rb331/Fz31JusL r44MxoKHzPpOB1GkSUYagGuff7LORB1mu/KAQyLAyZc1kqBuxoiqeBAJl5aUGHlN u8ipSPZzaKIehFL9Jv+xUv6CtoIR5VqsgEaVy1D6GI07bUq7vAjaKSp95zRBB7pb jmLYs2fSjgTJgRy/X6gr0YE3VEueFj+fiiwNpf1lgk4GzC+jbmOtWjfVHKJhLJMs 8MU77MoMBmuLUlZlfmaDEY8qw4JpFQ/XwHSJCj37yaXOCOXRxtpZfgcxGy7yUN4e ZF6wmpOEwCGiCZ2/Xn5nqlRzNZH8XSXO/VkCoUJJYTLf0Ym9cmRE6+5LCazQ== 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-sender:x-me-sender:x-sasl-enc; s=fm3; t= 1747927578; x=1748013978; bh=jg927pSIYPTcVjkFJZDCKmRqmJTHjZTuc4r C6Ej3D70=; b=vnm8m8DTp4olGwT7aug2nIF8lDv7jqhJ0MGEEu7hlcxBRrTvTIk MUsnjtuxhCifRLfBGx9xJAZfKru1ynRNbfijspkwZsD545Mcn/3lWWPLfFAPBhuf sDMec4AyDSGT2HQy1GnXYO+/SsY3jeTbwAsBBPWLyTPpqQt5hS3qTgYAwGDPM46F j/ShCXdUj7VmVj+Kc2O8GpGf6YuzIoAr4CB2l95OlM/FSwbfn3BHkffakK9A2BH/ MJ1tu6+Bkq16AekbXJcjAGXFkiV+EnA6ujVVZF5tSeI7A8+NimJ1D0n169RCh+2V SyVfbdKmE4sbKH1/Oi48XjviZkPtSlNeXiQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtddtgdeifedvucdltddurdegfedvrddttd dmucetufdoteggodetrfdotffvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdggtfgf nhhsuhgsshgtrhhisggvpdfurfetoffkrfgpnffqhgenuceurghilhhouhhtmecufedttd enucenucfjughrpefoggffhffvkfgjfhfutgesrgdtreerredtjeenucfhrhhomhepfdft ohgsucfnrghnuggvrhhsfdcuoehrohgssegsohhtthhlvggurdgtohguvghsqeenucggtf frrghtthgvrhhnpedtueejtdethfeulefhtdelieduteelffdtudelheffgedtieehhfel ieejgfevgeenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhroh hmpehrohgssegsohhtthhlvggurdgtohguvghspdhnsggprhgtphhtthhopedupdhmohgu vgepshhmthhpohhuthdprhgtphhtthhopehinhhtvghrnhgrlhhssehlihhsthhsrdhphh hprdhnvght X-ME-Proxy: Feedback-ID: ifab94697:Fastmail Received: by mailuser.phl.internal (Postfix, from userid 501) id 16A5A3020061; Thu, 22 May 2025 11:26:18 -0400 (EDT) X-Mailer: MessagingEngine.com Webmail Interface Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 X-ThreadId: Tadb98cb953d91cf3 Date: Thu, 22 May 2025 17:25:57 +0200 To: internals@lists.php.net Message-ID: <24654474-34d4-4605-8d9b-429b96d9a4a5@app.fastmail.com> In-Reply-To: References: Subject: Re: [PHP-DEV] [RFC] Readonly constructors Content-Type: multipart/alternative; boundary=d874f8861c39401caabf4242a69c931d From: rob@bottled.codes ("Rob Landers") --d874f8861c39401caabf4242a69c931d Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On Thu, May 22, 2025, at 17:01, Mihail Liahimov wrote: > In PHP 8.1 the class read-on syntax was added, which allows you to cre= ate classes that are completely closed to changes. In addition, it allow= s you to write less boilerplate when declaring a property in the constru= ctor. >=20 > Before 8.1: >=20 > class A > { > public function __construct( > private readonly int $a, > private readonly int $b, > private readonly int $c, > ) { } > } >=20 > After 8.1: >=20 > readonly class A > { > public function __construct( > private int $a, > private int $b, > private int $c, > ) { } > } >=20 > But there are also scenarios where, for some reason, not all propertie= s need to be readonly. For example, when we inherit from a non-readonly = class or when our class has a state for some kind of memoization. In thi= s case, the class is not readonly, and as a result, you have to write an= extra boiler plate in the constructor. I would like to be able to decla= re many readon properties in the constructor in a way similar to readon = classes. >=20 > I suggest adding the ability to declare a constructor as a readonly. I= n this case, all the properties declared in it will be automatically rea= donly. >=20 > class A extends NonReadonlyClass > { > public readonly function __construct( > private int $a, > private int $b, > private int $c, > ) { } > } >=20 > Yes, it looks a bit strange, but maybe someone will have other suggest= ions. Perhaps there is a more competent way to organize this idea. Hello Mihail, I feel like it is kinda that way on purpose? If you find yourself writin= g a constructor with a lot of parameters that are a mix or readonly prop= erties, it feels like that is a code smell. Either you should consider d= ecomposing the object into multiple single-purpose objects or rethink th= e architecture in some way. For example: > when our class has a state for some kind of memoization. For example, in this case, you'd wrap the memoization state inside a mut= able object and then use that as a readonly property. =E2=80=94 Rob --d874f8861c39401caabf4242a69c931d Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: quoted-printable
On Thu, May = 22, 2025, at 17:01, Mihail Liahimov wrote:
In PHP 8.1 the class read-o= n syntax was added, which allows you to create classes that are complete= ly closed to changes. In addition, it allows you to write less boilerpla= te when declaring a property in the constructor.

Before 8.1:

class A
{
&nb= sp;   public function __construct(
      &= nbsp; private readonly int $a,
        pri= vate readonly int $b,
        private read= only int $c,
    ) { }
}

After 8.1:

readonly class A
{=
    public function __construct(
  &= nbsp;     private int $a,
       = ; private int $b,
        private int $c,<= /div>
    ) { }
}

But t= here are also scenarios where, for some reason, not all properties need = to be readonly. For example, when we inherit from a non-readonly class o= r when our class has a state for some kind of memoization. In this case,= the class is not readonly, and as a result, you have to write an extra = boiler plate in the constructor. I would like to be able to declare many= readon properties in the constructor in a way similar to readon classes= .

I suggest adding the ability to declare a con= structor as a readonly. In this case, all the properties declared in it = will be automatically readonly.

class A extends= NonReadonlyClass
{
    public readonly fu= nction __construct(
        private int $a= ,
        private int $b,
 =       private int $c,
    ) { }
}

Yes, it looks a bit strange, but maybe = someone will have other suggestions. Perhaps there is a more competent w= ay to organize this idea.

He= llo Mihail,

I feel like it is kinda that w= ay on purpose? If you find yourself writing a constructor with a lot of = parameters that are a mix or readonly properties, it feels like that is = a code smell. Either you should consider decomposing the object into mul= tiple single-purpose objects or rethink the architecture in some way. Fo= r example:

when our c= lass has a state for some kind of memoization.
For example, in this case, you'd wrap the memoization state= inside a mutable object and then use that as a readonly property.
=

=E2=80=94 Rob
--d874f8861c39401caabf4242a69c931d--