Re: Readonly properties - immutability by default

From: Date: Sat, 17 Jul 2021 11:47:37 +0000
Subject: Re: Readonly properties - immutability by default
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-115452@lists.php.net to get a copy of this message
On 16.07.21 09:06, Nikita Popov wrote:
We cannot make properties readonly by default, because that would be a major backwards compatibility break. If you're going for brevity, something you can do is omit the visibility specifier, as it is public by default. "readonly int $prop" works.
Would it be possible to adapt constructor property promotion to support this (as far as I tell, it currently does not)? Namely: class A {
    public function __construct(
        readonly int $id,
    ) {
    }
} According to the constructor property promotion RFC, the promotion only happens when public, private and protected is used for an constructor parameter. With the readonly RFC accepted, it would make sense to also do constructor property promotion when readonly is used, to avoid the explicit public, because readonly would also clearly define the parameter as not-only-a-parameter.

« previous php.internals (#115452) next »