readonly class landed in PHP 8.2 and is the cleanest way yet to model immutable value objects — DTOs, money, coordinates, config snapshots. But “immutable” is a stronger word than the feature actually delivers, and a few rules will bite you if you assume otherwise. Here’s the practical version, verified on PHP 8.5.

PHP readonly classes — immutable value objects

The basics

Marking a class readonly makes every declared property readonly automatically — no need to repeat the keyword on each one:

<?php

readonly class Money
{
    public function __construct(
        public int $cents,
        public string $currency,
    ) {}
}

$m = new Money(100, 'USD');
echo $m->cents; // 100

$m->cents = 200; // Error: Cannot modify readonly property Money::$cents

Properties must be typed, can only be initialized once, and initialization must happen from the scope of the class that declared them. Constructor property promotion is the natural fit.

Gotcha 1: “immutable” is only shallow

A readonly property can’t be reassigned, but if it holds an object, that object’s own state is still mutable:

<?php

readonly class Box
{
    public function __construct(public ArrayObject $items) {}
}

$box = new Box(new ArrayObject([1, 2]));
$box->items->append(3); // perfectly legal
// $box->items is still [1, 2, 3]

Readonly freezes the reference, not the thing referenced. For genuinely immutable values, store DateTimeImmutable instead of DateTime, plain scalars/arrays instead of mutable collection objects.

Gotcha 2: no default values, and strict inheritance

Two compile-time restrictions that surprise people coming from regular classes:

  • A readonly property cannot have a default value — public int $x = 1; is a fatal error, even in a readonly class.
  • Inheritance is all-or-nothing: a readonly class may only extend a readonly class, and a normal class may not extend a readonly class.
<?php

readonly class P { public function __construct(public int $x) {} }
readonly class C extends P {} // OK

class Normal {}
// readonly class D extends Normal {} // Fatal: readonly cannot extend non-readonly
// class E extends P {}              // Fatal: non-readonly cannot extend readonly

Gotcha 3: withX() “with-ers” don’t work the obvious way

The usual immutable-update idiom is to clone and tweak one field. That fails here — reassigning a readonly property in a normal method is still rejected, even on a fresh clone:

<?php

readonly class User
{
    public function __construct(public string $name, public int $age) {}

    public function withName(string $new): static
    {
        $clone = clone $this;
        // $clone->name = $new; // Error: Cannot modify readonly property
        return new static($new, $this->age); // construct instead
    }
}

Because every property in a readonly class is readonly, you can’t stage a pending value on a helper property either. Two valid escape hatches:

  • Recommended: construct a new instance (new static(...)) as above.
  • PHP 8.3+: reinitialize a readonly property inside __clone() — the one place reassignment is explicitly allowed:
<?php

readonly class Counter
{
    public function __construct(public int $value) {}

    public function __clone()
    {
        $this->value++; // allowed since PHP 8.3
    }
}

$a = new Counter(5);
$b = clone $a;
// $a->value === 5, $b->value === 6

When to reach for it

  • Value objects & DTOs — request payloads, money, coordinates, config snapshots.
  • Anything you pass across boundaries where an accidental mutation would be a bug.

Skip it for entities that genuinely change over time, and remember that it protects against reassignment — pair it with immutable types (DateTimeImmutable, readonly collections) when object graphs are involved.

Readonly classes won’t make your domain immutable by magic, but for the common case of “a small bundle of typed values that should never change,” they remove an entire category of accidental mutation with almost no boilerplate.