---
title: "Immutability in PHP Beyond readonly"
url: https://daily.dev/posts/immutability-in-php-beyond-readonly-e2zuz4sl6
source_url: https://wendelladriel.com/blog/immutability-in-php-beyond-readonly
type: article
source: "W endell Adriel"
author: "Wendell Adriel"
published: 2026-08-14T10:25:48.462Z
updated: 2026-08-15T07:00:18.728Z
tags: ["php", "design-patterns"]
reading_time: 14
upvotes: 13
comments: 1
language: en
---

> ## Documentation Index
> Fetch the complete documentation index at: https://daily.dev/llms.txt
> Use this file to discover all available pages before exploring further.

# Immutability in PHP Beyond readonly

**[W endell Adriel](https://daily.dev/sources/wendelladriel)** · [@wendell_adriel](https://daily.dev/wendell_adriel) · 14 min read · 13 upvotes · 1 comments

## Summary

An in-depth look at PHP immutability that goes past the readonly keyword, explaining that readonly only prevents property reassignment but does not guarantee deep immutability. It covers interior mutability pitfalls with mutable objects like DateTime, why immutable objects should only contain immutable building blocks (DateTimeImmutable, enums, value objects), returning new values instead of using setters, safely designing immutable arrays and collections, normalizing mutable input at application boundaries, why cloning is not an immutability strategy, pairing immutability with constructor invariants, testing the contract rather than the keyword, and when mutability (e.g. Eloquent models) is still the right choice. Ends with a practical checklist for verifying true immutability.

## Full article

daily.dev links to this article rather than hosting it. Read it at the original source: <https://wendelladriel.com/blog/immutability-in-php-beyond-readonly>

## Questions this post answers

### Does PHP's readonly keyword make an object fully immutable?

No, readonly only prevents a property from being reassigned after its first assignment; it does not make nested objects immutable. A readonly property can still hold a mutable object, such as a DateTime instance, and calling a mutating method on that nested object changes the observable state even though the container itself was never reassigned. This is called interior mutability.

_Developers hardening PHP domain models can compare readonly patterns and pitfalls on daily.dev._

### Should I use DateTime or DateTimeImmutable inside a PHP value object?

Use DateTimeImmutable, since its modifying methods like modify() return a new instance and leave the original untouched, unlike DateTime whose methods mutate the object in place. Storing a DateTime inside a readonly class still allows its internal state to change after construction, breaking the immutability guarantee the value object is supposed to provide.

_daily.dev helps developers track best practices for building safe PHP value objects._

### Is cloning an object in PHP enough to create a safe independent copy?

No, PHP's default clone is shallow, meaning the outer object is copied but nested object references are shared between the original and the copy. Modifying a nested property, such as an Address inside an Invoice, through the cloned object also changes the original unless __clone() is implemented to explicitly clone every mutable child, array of objects, and future property.

_Developers debugging shared-reference bugs after cloning can dig into PHP object patterns on daily.dev._

## Community discussion

Top comments from developers on daily.dev.

**@agustinbarrientos** · 0 upvotes

> Nested arrays should be cloned before the value object gets called immutable

---

Tags: [#php](https://daily.dev/tags/php), [#design-patterns](https://daily.dev/tags/design-patterns)

[View this post on daily.dev](https://daily.dev/posts/immutability-in-php-beyond-readonly-e2zuz4sl6)
