[php-src] PR #20996: Allow promoted readonly property to be reassigned once in constructor
| From: | nicolas-grekas | Date: | Wed, 21 Jan 2026 19:34:17 +0000 |
| Subject: | [php-src] PR #20996: Allow promoted readonly property to be reassigned once in constructor | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-34626@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/20996
Author: nicolas-grekas
I find it quite annoying that readonly properties play badly with CPP (constructor property
promotion).
Doing simple processing of any argument before assigning it to a readonly property forces opting out
of CPP.
This PR allows setting once a readonly property in the body of a constructor after the property was
previously set using CPP.
Before this PR:
```php
class Point {
public readonly float $x;
public readonly float $y;
public function __construct(
float $x = 0.0,
float $y = 0.0,
) {
$this->x = abs($x);
$this->y = abs($y);
}
}
```
After this PR:
```php
class Point {
public function __construct(
public readonly float $x = 0.0,
public readonly float $y = 0.0,
) {
$this->x = abs($x);
$this->y = abs($y);
}
}
```
This allows keeping properties declaration in its compact form.
I'll submit an RFC of course but would also welcome early feedback here before.