In PHP 8.1, the readonly keyword is now available. I am curious about its purpose. Is it intended to assist the editor in recognizing that a property is solely readonly, to aid the client in understanding this characteristic, or does it serve another purpose?
readonly
properties allow you to create immutable objects, or at the very least immutable properties (since 8.2 we also have readonly
for classes, which allow creating immutable objects without having to apply the keyword to each of the class properties).
That way, you can be sure that a value won't be changed by accident after being initialized, throughout the object's life.
It's a very similar concept to constants (set via const
or define
), albeit with two important differences:
readonly
properties will be set during runtime, usually during on object instantiation (so multiple instances will be able to hold different values*)You could achieve the same with a private property only accessible via a getter. E.g., in "the olden days":
class Foo {
private DateTimeImmutable $createAt;
public function __construct() {
$this->createdAt = new DateTimeImmutable();
}
public function getCreatedAt(): DateTimeImmutable
{
return $this->createdAt;
}
}
$f = new Foo();
echo $f->getCreatedAt()->format('Y-m-d H:i:s');
The only problem with this is that it requires a lot of boilerplate code.
With PHP 8.1, almost the same could be achieved by doing:
class Foo
{
public function __construct(
public readonly DateTimeImmutable $createdAt = new DateTimeImmutable()
)
{ }
}
$f = new Foo();
echo $f->createdAt->format('Y-m-d H:i:s')
And since PHP 8.2 add readonly
classes, it gets even better, since one can do:
readonly class Foo
{
public function __construct(
public string $name,
public DateTimeImmutable $createdAt = new DateTimeImmutable()
)
{ }
}
And now both Foo::name
and Foo::createdAt
are readonly
, and can only be set during object instantiation.