Skip to content

Conversation

@nicolas-grekas
Copy link
Contributor

@nicolas-grekas nicolas-grekas commented Jan 21, 2026

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 (CPP not possible):

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:

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.

@nicolas-grekas nicolas-grekas changed the title Allow promoted readonly property to be reassigned once in constructor Allow promoted readonly properties to be reassigned once in constructor Jan 21, 2026
@nicolas-grekas nicolas-grekas force-pushed the reinit-readonly-constructor branch 4 times, most recently from 1667954 to e8009a7 Compare January 22, 2026 14:35
@nicolas-grekas
Copy link
Contributor Author

Implementation is now ready.
Team work with Claude Code opus 4.5 💪
The CI is green (the failures are about CI tools that couldn't be installed, unrelated to the patch)

@nicolas-grekas
Copy link
Contributor Author

@nicolas-grekas nicolas-grekas force-pushed the reinit-readonly-constructor branch from e8009a7 to 3ccacde Compare January 22, 2026 17:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants