readonly (PHP 8.1) is the cheapest way to make a value object actually behave like a value object. It is also routinely misunderstood: teams add readonly to a property, assume the whole object is now immutable, and then spend an afternoon debugging state that changed anyway.

readonly protects one write slot. That’s it. Everything else below follows from that single sentence.

Diagram: readonly locks the property slot, but a nested object inside it stays mutable

What readonly actually guarantees

A readonly property can be written exactly once, and only from inside the scope of the class that declares it.

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

$price = new Money(1000, 'EUR');

$price->amount = 2000;
// PHP Error: Cannot modify readonly property Money::$amount

Two details that surprise people:

$price->amount = 1000;   // same value — STILL an Error. A write is a write.
unset($price->amount);   // Error: Cannot unset readonly property Money::$amount

And the declaration itself has rules:

class Bad
{
    public readonly int $x = 5;   // Fatal: Readonly property cannot have default value
    public readonly $y;           // Fatal: must have a type
    public static readonly int $z; // Fatal: cannot be static
}

No default value is the one that trips people up most: the value must come from a runtime assignment, which in practice means the constructor.

Gotcha 1: readonly is shallow

This is the big one. readonly freezes the property, not what the property points at. If the value is an object, that object is still fully mutable.

final class HttpRequest
{
    public function __construct(
        public readonly ArrayObject $headers,
    ) {}
}

$req = new HttpRequest(new ArrayObject(['accept' => 'application/json']));

$req->headers = new ArrayObject();       // Error — can't repoint the slot
$req->headers['accept'] = 'text/html';   // Works. The object mutates happily.

Nothing was reassigned in the second line — a method was called on the object the slot already held, so readonly has no say. Your “immutable” request now reports a different Accept header to whoever holds a reference to it.

Arrays behave differently, because arrays are value types in PHP:

final class Config
{
    /** @param array<string,string> $values */
    public function __construct(public readonly array $values) {}
}

$c = new Config(['env' => 'prod']);

$c->values['env'] = 'dev';  // Error: Cannot modify readonly property Config::$values
$c->values[] = 'extra';     // Error too — indirect modification counts as a write

So the rule is: readonly + scalar/array = genuinely immutable. readonly + object = only the reference is frozen. If you need deep immutability, hold immutable types (DateTimeImmutable, not DateTime), or clone on the way in:

public function __construct(DateTime $createdAt)
{
    $this->createdAt = DateTimeImmutable::createFromMutable($createdAt);
}

Gotcha 2: initialization is scope-bound, not visibility-bound

public readonly does not mean “anyone can write it once”. Only the declaring class may perform that first write.

class Entity
{
    public readonly int $id;
}

$e = new Entity();
$e->id = 7;
// Error: Cannot initialize readonly property Entity::$id from global scope

Subclasses are outside that scope too:

class Child extends Entity
{
    public function __construct()
    {
        $this->id = 7;
        // Error: Cannot initialize readonly property Entity::$id from scope Child
    }
}

This is why external hydrators (ORMs, serializers, DTO mappers) can’t just assign readonly fields — they have to go through the constructor or use reflection. If you subclass, declare the property in the class that also initializes it.

Gotcha 3: clone-with is not “clone then assign”

The natural immutable-update helper looks like this — and it fails on every version:

public function withAmount(int $amount): static
{
    $clone = clone $this;
    $clone->amount = $amount;   // Error: Cannot modify readonly property
    return $clone;
}

PHP 8.3 relaxed exactly one case: a readonly property may be reassigned inside __clone(). That makes deep cloning possible, but it does not enable arbitrary clone-then-mutate from outside.

final class Booking
{
    public function __construct(
        public readonly DateTime $slot,   // mutable value, see Gotcha 1
    ) {}

    public function __clone(): void
    {
        $this->slot = clone $this->slot;  // allowed since PHP 8.3
    }
}

For the actual “copy with one field changed” pattern, the portable answer is a fresh instance:

public function withAmount(int $amount): static
{
    return new static($amount, $this->currency);
}

Verbose with many fields, but explicit and works on 8.1 through 8.4. PHP 8.5 added native clone-with syntax; until your whole fleet is on it, new static(...) is the safe default.

Gotcha 4: readonly kills lazy initialization

Any memoization pattern that writes the property on first access is incompatible:

private readonly Report $report;

public function report(): Report
{
    return $this->report ??= $this->build();  // works ONCE, then Errors
}

It survives the first call and blows up on the second. If you need caching, keep the cache in a normal private property and expose it through a method — don’t mark it readonly.

Cheat sheet

  • readonly = write-once, from the declaring scope only. Same-value writes and unset() also throw.
  • Must be typed; no default value; never static.
  • Scalars and arrays are truly protected. Objects are not — only the reference is.
  • Prefer immutable value types inside readonly props (DateTimeImmutable, enums, other readonly classes).
  • readonly class (PHP 8.2) marks every property readonly and blocks dynamic properties — good for DTOs and value objects.
  • Deep-clone inside __clone() (8.3+); build copies with new static(...) everywhere else.
  • Don’t use readonly for lazily computed or cached state.

Used with that model in mind — a lock on the slot, not on the graph — readonly removes a whole class of accidental-mutation bugs with zero runtime cost.