Discuss the trade-offs of using readonly classes versus traditional private properties with getters.

Last updated 15 June 2026

  • readonly classes enforce immutability at the language level (set once during initialization) with little boilerplate, so properties can be public yet safe. Ideal for value objects and DTOs.

  • Trade-offs of readonly: no transformation layer, changes require clone/withers, and values can't be lazily computed or cached after construction.

  • Private properties with getters keep encapsulation, letting the internal representation evolve without breaking the public API. Use them for computed values or API stability.

  • https://stitcher.io/blog/new-in-php-82
  • https://phpfashion.com/en/php-readonly-properties
  • https://doganoo.medium.com/readonly-classes-in-php-a-useful-addition-to-readonly-properties-4457b27a9f16
All PHP interview questions