PHP 8.1 introduces readonly
class properties. For example, before I would write:
class User
{
public string $name;
public function __construct(string $name) {
$this->name = $name;
}
public getName(): string {
return $this->name;
}
}
But now I can do:
class User
{
public function __construct(
public readonly string $name,
) {}
}
The latter is obviously much more compact, so I am wondering if this syntax completely replaces the former for every use case, or are there still cases where the "old" syntax should be preferred?