Nick Sdot has proposed an RFC to allow default values for readonly class properties in PHP. The change is described as a minimal technical modification—removing just five lines of code—that enables constant-like behaviour with strict contracts without requiring constructor assignment.
During the review process, discussions with Tim Düsterhus focused on technical edge cases. Clarifications were added regarding the interaction with the "clone-with" feature and asymmetric visibility. Specifically, the RFC now clarifies that using public(set) visibility provides a more accurate example than private(set).
Significant attention was given to unserialization behavior. Because a readonly property with a default value is already initialized when __unserialize is called, assigning to it during object hydration will fail. This differs from the current behavior around readonly properties during unserialization.
Comments
No comments yet — be the first.
Open the discussion
No account or password needed — just enter your e-mail and we’ll send you a one-time sign-in link. First time here? You’re set up automatically.
Your rating will be applied automatically after you sign in.
Check your inbox
We’ve sent a sign-in link to …. Open it on this device — this tab will sign you in automatically.
Waiting for your click …
·