Freek Van der Herten1 min readintermediate
Immutability in PHP Beyond readonly
Summary
The article walks through practical ways to achieve immutability in PHP beyond the built‑in readonly keyword, covering interior mutability, immutable value objects, collections, dates, and cloning. It also shows how to enforce and test these patterns in real codebases.
- Readonly properties give shallow immutability; they prevent reassignment but not mutation of referenced objects.
- Avoid interior mutability by never exposing mutable references from value objects or collections.
- Implement true immutable value objects by making all fields private, final, and returning new instances on changes.
- Use cloning or named constructors to create modified copies without altering the original instance.
PHP developers who want more predictable, side‑effect‑free code should read this to learn enforceable immutability techniques.
6/10




