Readonly classes extend the idea of immutability from individual properties to the entire object.
They let us express that an object is a value
— something defined entirely by its construction and never changed afterward.
They reflect a broader shift in modern PHP:
→immutability should be intentional, visible, and effortless.
A readonly class is declared with the readonly keyword:
readonly class Point { public function __construct( public float $x, public float $y, ) {} }
Every property is implicitly readonly.
Every property must be initialized in the constructor.
No property may change afterward.
The object becomes a stable value.
Readonly classes reduce:
They make value objects feel natural and honest.
A readonly class is a value with a name.
It’s not an object that happens to be immutable.
It’s an object that declares immutability as part of its identity.
The key idea:
Without readonly classes:
class Point { public readonly float $x; public readonly float $y; public function __construct(float $x, float $y) { $this->x = $x; $this->y = $y; } }
With readonly classes:
readonly class Point { public function __construct( public float $x, public float $y, ) {} }
Same meaning.
Less ceremony.
More clarity.
Use them when:
Readonly classes shine in:
Avoid them when:
Readonly classes are for values,
not actors.
AI often marks classes as readonly even when they represent evolving state. Sometimes it mixes readonly classes with setters, or tries to mutate properties after construction.
Our mental model helps us see when a class expresses a true value
— and when AI is using readonly simply because it “looks safe”.
Readonly classes let us declare immutable objects with clarity and intention.
They reduce noise, prevent accidental mutation,
and make value objects feel natural.
Once we internalize this,
we can immediately see when AI‑generated code:
Readonly classes are a small feature
— but they teach us
how modern PHP wants to express values with honesty and precision.