Skip to content

MIR0601

ReadonlyPropertyAlreadyInitialized

A readonly property is being assigned a value after it has already been initialized. Readonly properties can only be assigned once, typically in the constructor.

<?php
class User {
public readonly string $name;
public function __construct(string $name) {
$this->name = $name;
}
public function rename(string $newName): void {
$this->name = $newName; // Error: readonly property already initialized
}
}

Remove the assignment or use a different design pattern:

<?php
class User {
public readonly string $name;
public function __construct(string $name) {
$this->name = $name;
}
// Instead of modifying, create a new instance
public function withName(string $name): self {
return new self($name);
}
}