MIR1705
ImmutablePropertyModification
An attempt to modify a property on an object that is marked as immutable (readonly class or readonly property).
Example
Section titled “Example”<?phpclass User { public readonly string $name;
public function __construct(string $name) { $this->name = $name; }}
$user = new User('Alice');$user->name = 'Bob'; // Error: cannot modify readonly propertyHow to fix
Section titled “How to fix”Create a new instance with the updated value instead of modifying the existing one:
<?phpclass 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');