Readonly Properties and Classes
Readonly properties and readonly classes were introduced in PHP versions 8.1 and 8.2 respectively...but what are they?
A readonly property is one that can be initialized once and cannot be modified afterwards...therefore they are immutable properties. You can make individual properties readonly with this syntax
readonly public string $someProperty;
If, however, you want to make all of the properties on the class readonly, you can simply make the class readonly, like so:
readonly class SomeClass {}
The properties on our UserRegistration DTO should not change once initialized so let's make the class readonly
Branch: https://github.com/GaryClarke/learn-oophp/tree/118-readonly-properties-and-classes
0 comments