Understanding PHP 8 Readonly Properties: Definition, Benefits, and Usage
PHP 8 introduces readonly properties that can only be set during object initialization, providing immutable, constant-like behavior useful for defining constants, protecting sensitive data, and enhancing code safety, with examples showing initialization, immutability, consistency, and even getter/setter usage.
Readonly properties are a game‑changing feature in PHP 8 that allow you to declare properties that can only be set during initialization and cannot be modified thereafter.
They act as immutable guardians in code, ensuring values stay unchanged. Typical uses include defining constants, preventing accidental modification of sensitive data, and improving code safety and reliability.
Defining constants
Preventing accidental modification of sensitive data
Improving code safety and reliability
If you haven’t tried readonly properties yet, you’re encouraged to give them a try because they bring many benefits to your code.
In PHP 8, readonly properties give classes a new possibility: they can only be set during construction, effectively adding a solid, unchanging stone to your code.
<code>class UserProfile {
public readonly string $username;
public function __construct(string $username) {
$this->username = $username;
}
}
</code>In this example, $username is a readonly property; once set in the constructor its value never changes.
Tips and Tricks
1. Initialise with a default value
Readonly properties can only be set during initialization. If you cannot guarantee a default or initial value, you can make the readonly property nullable.
<code>class UserProfile {
public readonly string $username;
public readonly ?string $bio;
public function __construct(string $username, ?string $bio = null) {
$this->username = $username;
$this->bio = $bio;
}
}
</code>Here the $bio property may be null.
2. Achieve immutability
Readonly properties can be used to enforce immutability. Once set, the property cannot be changed, ensuring data integrity.
<code>class ImmutableDate {
public readonly DateTimeImmutable $date;
public function __construct(DateTimeImmutable $date) {
$this->date = $date;
}
}
</code>In this case $date is immutable.
3. Ensure consistency
Readonly properties can be derived from other properties to keep an object consistent. For example:
<code>class Circle {
public readonly float $radius;
public readonly float $area;
public function __construct(float $radius) {
$this->radius = $radius;
$this->area = $this->calculateArea();
}
private function calculateArea(): float {
return pi() * $this->radius * $this->radius;
}
}
</code>Here $area is calculated from $radius ; once $radius is set, $area is determined.
Unexpected surprise: getters and setters for readonly properties
Although readonly properties can only be set during initialization, they can still have getters and setters. This may seem contradictory, but it is allowed and offers several advantages.
Consider this simple example:
<code>class MyClass {
public readonly string $immutableProperty;
public function __construct(string $value) {
$this->immutableProperty = $value;
}
public function getImmutableProperty(): string {
return $this->immutableProperty;
}
public function setImmutableProperty(string $newValue): void {
$this->immutableProperty = $newValue;
}
}
</code>Even though $immutableProperty is readonly, we can still access its value via a getter and modify it via a setter, because the setter operates during construction or within the class scope.
Readonly properties act like a reliable friend: once set, they stay unchanged, making your code more stable and preventing unexpected changes.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.