Hi Internals,

I'd like to propose the idea of adding support for immutable/final/readonly
properties in PHP 8.

My plan is to add a new immutable/final/readonly (the name is up for
debate) property modifier to the language so that these kind of properties
could only be initialized but not modified afterwards. Unlike how final
properties in Java work, I think it would be beneficial to allow lazy
initialization of these properties after object construction have taken
place. Currently, I'm in favour of using final or readonly, but not yet
very sure.

I'll write a proper RFC in the following days/week. My clear intent with
final properties is to pave the road for immutable objects for which I have
a POC pull request open (but I haven't pushed all my work yet).

So far, my biggest question (apart from the name) have been how non-typed
properties should behave: as they are implicitly initialized to null if
they don't have an explicit default value (while typed properties remain
uninitialized), further modifications would be impossible to do on them -
which would make non-typed final properties almost useless. Nikita
suggested to just avoid their initialization, but I'd be curious about
other ideas as well.

Thanks,
Máté Kocsis

Reply via email to