Re: [PHP-DEV] [RFC] Readonly Property Defaults

[email protected] (Tim Düsterhus) Thu, 09 Jul 2026 15:12:31 +0200
Newsgroups php.internals
Message-ID <[email protected]>
Hi

On 2026-07-09 13:58, Nick Sdot wrote:
> At the time `__unserialize` is called the object exists, and the 
> property is initialised with the default value. So it fails in the same 
> way a re-assignment in the constructor or reflection `setValue()` will 
> fail. So, I wouldn't say "not a situation we have so far", but agree 
> that spelling it out cannot be wrong. RFC section is updated, and your 
> PR comment example added as a test!

Yes, it would be a situation we do not yet have so far: As far as I can 
tell there is no way for a readonly property to be preinitialized when 
unserialization happens. This means that it is not not observable that 
the non-hooked serialization path ignores `readonly`. What is 
effectively guaranteed right now is that using a custom 
`__unserialize()` hook that loops over the given array and assigns all 
properties will behave identically to non-hooked serialization. This 
would no longer be the case as of this RFC.

There is two options here:

- The non-hooked unserialization path must also error out when trying to 
unserialize into a readonly property with a default value.
- __unserialize() must be able to unserialize into a readonly property 
with a default value, similarly to __clone().

But the current situation of “__unserialize() is less powerful than the 
standard unserialization” is not acceptable, because it prevents 
introducing `__unserialize()` on a class with a readonly property with 
default values.

Best regards
Tim Düsterhus