PHP 8.2 introduced readonly classes: classes where all properties are automatically readonly, preventing modification after construction.
The use case I reach for most: Value Objects. A Money object that represents a specific amount in a specific currency should be immutable — once constructed, £14.99 should always be £14.99, never silently mutated by a function that modifies it in place.
Without readonly classes, enforcing immutability required either manual clone() patterns or trusting that no code would mutate the object. With readonly classes, the PHP engine enforces immutability at the language level.
The secondary benefit: readonly classes make code easier to reason about. A function that accepts a readonly object can be understood in isolation — the object it receives will be identical when the function returns. No hidden state changes.
I am adopting readonly classes for all Value Objects in SellTrove's domain model: Money, ProductPrice, OrderStatus, TenantId. The language now enforces the invariants the domain model requires.
— Dick Bassey | DevDick | 2025