Hi
Am 2025-05-15 00:04, schrieb Larry Garfield:
Subtle point here. If the __clone() method touches a readonly property, does that make the property inaccessible to the new clone-with?
Yes. Quoting from the RFC:
The currently linked implementation “locks” a property if it modified within __clone(), if this is useful is up for debate.
-
A single unlock block would be confusing to me.
We’ve implemented it like that, because it felt most in line with what was decided in https://wiki.php.net/rfc/readonly_amendments#proposal_2readonly_properties_can_be_reinitialized_during_cloning, which says:
Reinitialization of each property is possible once and only once:
We expect “public(set) readonly” + “__clone()” to be rare and from within the class, the author knows how their __clone()
implementation works and can make sure it is compatible with whatever properties they might want to update during cloning. The lack of “use cases” is the primary reason we made the more conservative choice, but we are not particularly attached to this specific behavior.
Best regards
Tim Düsterhus