Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:127427 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 93FBA1A00BC for ; Thu, 22 May 2025 15:01:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1747925966; bh=lXH0dpv9fpgTdXmDgy8VhwPnswHD3s9Sqgir8HCAJ10=; h=From:Date:Subject:To:From; b=McJT9nzcKh1j69F3lYR+OyWhlf6R57wQsXVmJalqFVQdNzEi5PH8sHFkV3d/qpuX5 N0+1hjjJq26MBzLVMv2QMGPvNH4GqARbiUG9VkKB9C/S0pojPbG/wjzSVUXw+8svWH vFNPIH7eb+q40xz/eKQ5F/0/KlBVcRYaRtVha4DXRvdrSCvygwYBBSjxhKkoCg3NqL dIHmpfYPNP0Nwr/SrFpCWkhMPsSUU8bOlamey1lSMq4l2HCi/O9Osjc0XS2A9QIJNG Xo2bfEL5bSy1mwG0Qa2pK9SUaTJn9E0ou3HCHUwK6SHvhvu0x1WpZoKVVTt1rI1/Q1 5FTcAT0KILMag== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4CFE6180077 for ; Thu, 22 May 2025 14:59:26 +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.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, 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: <91liahim@gmail.com> Received: from mail-yb1-f171.google.com (mail-yb1-f171.google.com [209.85.219.171]) (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 14:59:23 +0000 (UTC) Received: by mail-yb1-f171.google.com with SMTP id 3f1490d57ef6-e75668006b9so8400874276.3 for ; Thu, 22 May 2025 08:01:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747926090; x=1748530890; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=gnxqNr60N9TnqYE88QJun2lycZBGOPM96OfTagxugEY=; b=EAlrJZAP5u5bTMt3Sa5IsG0rVxe+NHzCGRqfPYDYoPGk0pPT5G1od6JJCieXrbd3iI iYc/HPzx1IN7QO9Uv7MPIPLjfsorKEWOq2NakXj1VHHxIcQ5ZvmT6kaRwfbFcqwRyfhb pXVASk2EkCY14QSEqBthDenxh5fod2EzaQC4klLMNrqezdJWrY836ggKPbG8T3LsQbEn WSdqYU+LRFFujVQzMws7z1Piy6qd7G8xXTftKDzo5WP37sIlgk6JBUQkL8B3tBcZfGsO sMbGlohCTvf7Ms1qVi0o3Y3UguSvfCfDM+rZQPH9l/tIWd8pT7iPMhLJgbRWIiHbbWl/ +58w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747926090; x=1748530890; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=gnxqNr60N9TnqYE88QJun2lycZBGOPM96OfTagxugEY=; b=CiyICCQAuW6NHGt3vXbnpqHa9ZG4SGw3TrviPY8rjpPJB8bAmfyurBShsk9R7ubBM/ HsMwZYwYpscnfJwt0R9afMhIR+ryjF7tESqODL9xaQU5FA8i1PSK8oynWabZ0SAZSbAo c6tmYx4FiYDDs3eQEWRo+i+/sngIj6uzce7M3rJQKUXO2mTwTiZZv5cfnTPUL5ul7JQh DlA43LIs/wf45isWpYgVBFfhZ+CF620itynOEPvhzyMDGPsXwZWNp6DeidmUfhniUcT7 Oy0X9b/P4iXHr/AsPGm8LUmYFPTpcmX02+mG23Ah4BEmcpA3V2aRSD9mMLM/bMcycs6W 7n2g== X-Gm-Message-State: AOJu0YzjrxkbEfAQ8+EYTpgGDFGbkJS9PUhckdq8hC8AEjpjriKbn5b4 cUilVkobB+Isg60uOi8H3t2YYjeGE7FAe8pahMRgk0DHc3f6N0Ee68LQzGCibbe8l9KbYWC7lXX nnxr2E04CWUHqxLtAaLTt7NDfEYPEpIYwGw1R X-Gm-Gg: ASbGncuNfGzuUHEMOWOaW4gEvmDLmB2X78FcVj+rqeY/JSUv0aNrOI6MnA/eURr0MKg gYcepDd6sg99fS4So354OlXeKQAbNX/gNUE4QTXbeJrFshcLRK/30csuKuIpNaWdqM9n19K+R01 DqBvVcWgZBjl5MN2Xh2y8IXJov0m9+sb7s X-Google-Smtp-Source: AGHT+IEEE+yjKQ4BtAnaUAsh+AiKsudq1w0yiQHIQCSqOb5X54YOpCqcpUfF1ICHdEC2Q9OQT2zOIZSEyVfrE/wHe28= X-Received: by 2002:a05:6902:1106:b0:e7d:672a:64f4 with SMTP id 3f1490d57ef6-e7d672a661emr5493027276.24.1747926090044; Thu, 22 May 2025 08:01:30 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 Date: Thu, 22 May 2025 20:01:17 +0500 X-Gm-Features: AX0GCFsttMDpuWFp4rVS1CXx_Wt1mn4aISSUXZqw5uPP2vXDFkXaUi9oTlK3J7k Message-ID: Subject: [PHP-DEV] [RFC] Readonly constructors To: PHP internals Content-Type: multipart/alternative; boundary="000000000000b0e2ba0635bac188" From: 91liahim@gmail.com (Mihail Liahimov) --000000000000b0e2ba0635bac188 Content-Type: text/plain; charset="UTF-8" In PHP 8.1 the class read-on syntax was added, which allows you to create classes that are completely closed to changes. In addition, it allows you to write less boilerplate when declaring a property in the constructor. Before 8.1: class A { public function __construct( private readonly int $a, private readonly int $b, private readonly int $c, ) { } } After 8.1: readonly class A { public function __construct( private int $a, private int $b, private int $c, ) { } } But there are also scenarios where, for some reason, not all properties 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 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 constructor as a readonly. In this case, all the properties declared in it will be automatically readonly. class A extends NonReadonlyClass { public readonly function __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 way to organize this idea. --000000000000b0e2ba0635bac188 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
In PHP 8.1 the class read-on syntax was added, which allow= s you to create classes that are completely closed to changes. In addition,= it allows you to write less boilerplate when declaring a property in the c= onstructor.

Before 8.1:

class A
{
=C2=A0 =C2=A0 public = function __construct(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 private readonly int $= a,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 private readonly int $b,
=C2=A0 =C2=A0= =C2=A0 =C2=A0 private readonly int $c,
=C2=A0 =C2=A0 ) { }
}

= After 8.1:

readonly class A
{
=C2=A0 =C2=A0 public function __= construct(
=C2=A0 =C2=A0 =C2=A0 =C2=A0 private int $a,
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 private int $b,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 private int $c= ,
=C2=A0 =C2=A0 ) { }
}

But there are also scenarios where, fo= r some reason, not all properties need to be readonly. For example, when we= inherit from a non-readonly class or when our class has a state for some k= ind of memoization. In this case, the class is not readonly, and as a resul= t, 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 s= imilar to readon classes.

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

class A extends NonReadonlyClass
= {
=C2=A0 =C2=A0 public readonly function __construct(
=C2=A0 =C2=A0 = =C2=A0 =C2=A0 private int $a,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 private int $b= ,
=C2=A0 =C2=A0 =C2=A0 =C2=A0 private int $c,
=C2=A0 =C2=A0 ) { }
= }

Yes, it looks a bit strange, but maybe someone will have other sug= gestions. Perhaps there is a more competent way to organize this idea. --000000000000b0e2ba0635bac188--