Skip to content

MIR1705

ImmutablePropertyModification

An attempt to modify a property on an object that is marked as immutable (readonly class or readonly property).

<?php
class User {
public readonly string $name;
public function __construct(string $name) {
$this->name = $name;
}
}
$user = new User('Alice');
$user->name = 'Bob'; // Error: cannot modify readonly property

Create a new instance with the updated value instead of modifying the existing one:

<?php
class User {
public readonly string $name;
public function __construct(string $name) {
$this->name = $name;
}
public function withName(string $name): self {
return new self($name);
}
}
$user = new User('Alice');
$updated = $user->withName('Bob');