PHP readonly Properties
last modified November 25, 2026
The PHP readonly keyword marks a property that can be assigned once and never changed. It was introduced in PHP 8.1 for individual properties, and PHP 8.2 extended it to whole classes. Readonly properties make value objects and data transfer objects predictable, because their state cannot drift after construction.
This article shows how a readonly property is initialized, which operations fail, how promotion and readonly classes work, and what the modifier does not protect.
Basic definitions
A readonly property can be assigned once and cannot be changed afterwards. The modifier was added in PHP 8.1 for single properties, and PHP 8.2 added readonly classes, where every declared property is readonly.
A readonly property must have a type. A default value is rejected, because it would count as the one initialization and leave the constructor no way to set the property. The value is normally set in the constructor, and the write is allowed only inside the class hierarchy.
Since PHP 8.4 a readonly property also has protected(set) visibility, which is the asymmetric visibility feature: the declaring class and its subclasses may write it, and nobody else. Before PHP 8.4 the write was restricted to the declaring class alone. In both versions a public readonly property cannot be initialized from outside the hierarchy; the public part is the read access.
The keyword protects the property, not the value. An array stored in a readonly property cannot be appended to, because that would modify the property indirectly, but an object stored there keeps its own mutable properties.
A readonly property
The first example declares a readonly name and assigns it in the constructor. After construction the value is fixed.
<?php
declare(strict_types=1);
class User
{
public readonly string $name;
public function __construct(string $name)
{
$this->name = $name;
}
}
$user = new User("Alice");
echo $user->name, "\n";
The assignment in the constructor is the only write the class performs. The property is not initialized at declaration, so the constructor is the single place that decides the value. A second assignment anywhere in the class would raise an Error, and the object becomes immutable with respect to that field.
$ php read_basic.php Alice
Assignment after construction fails
An attempt to change the value raises an Error that names the class and the property.
<?php
declare(strict_types=1);
class User
{
public function __construct(public readonly string $name)
{
}
}
$user = new User("Alice");
try {
$user->name = "Bob";
} catch (Error $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
}
The message is Cannot modify readonly property User::$name. It is an Error, so a test can catch it, but in normal code it signals a design mistake: the class exposed a property as mutable when it is not. Removing the property with unset is rejected in the same way, because that would be another way to change the object's state. Code that needs a new value should create a new object instead.
$ php read_modify.php Error: Cannot modify readonly property User::$name
Readonly promoted parameters
Constructor property promotion declares a property and assigns it in one step, and the promoted property can be readonly. A default value on the parameter is still allowed, because it is a parameter default rather than a property default.
<?php
declare(strict_types=1);
class Money
{
public function __construct(
public readonly int $amount,
public readonly string $currency = "EUR"
) {
}
}
$price = new Money(1250);
echo $price->amount, " ", $price->currency, "\n";
The Money object receives both values from the constructor argument list and never changes them. The default EUR applies when the caller omits the currency. This is the usual shape of a small value object: a short declaration, no setters, and a value that is safe to pass around and compare.
$ php read_promoted.php 1250 EUR
A readonly class
A class marked readonly makes every declared property readonly, so the modifier is written once instead of on each property. Such a class cannot declare static properties.
<?php
declare(strict_types=1);
readonly class Point
{
public function __construct(
public int $x,
public int $y
) {
}
}
$point = new Point(2, 5);
echo "({$point->x}, {$point->y})\n";
try {
$point->x = 9;
} catch (Error $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
}
Both coordinates are readonly without repeating the modifier, and the assignment in the script raises the usual Cannot modify readonly property Point::$x. A readonly class is the shortest way to express an immutable value object. The restriction on static properties follows from the meaning of the keyword: a static property belongs to the class and would be mutable shared state.
$ php read_class.php (2, 5) Error: Cannot modify readonly property Point::$x
Extending a readonly class
Only a readonly class may extend a readonly class. The child inherits the restriction and adds its own readonly properties.
<?php
declare(strict_types=1);
readonly class Base
{
public function __construct(public string $id)
{
}
}
readonly class Child extends Base
{
public function __construct(string $id, public string $label)
{
parent::__construct($id);
}
}
echo (new Child("a1", "Widget"))->id, "\n";
The child constructor passes the identifier to parent::__construct, which performs the one allowed assignment for $id, and then the promoted $label is assigned by the promotion. A non-readonly child would be rejected with Non-readonly class Child cannot extend readonly class Base, because it could add a mutable property and break the guarantee the parent type makes.
$ php read_inherit.php a1
What readonly does not protect
Readonly freezes the property slot, not the data behind it. The next example stores an array and an object and tries to change both.
<?php
declare(strict_types=1);
class Meta
{
public array $tags = [];
}
class Document
{
public function __construct(
public readonly array $lines,
public readonly Meta $meta
) {
}
}
$doc = new Document(["intro"], new Meta());
$doc->meta->tags[] = "draft";
echo $doc->meta->tags[0], "\n";
try {
$doc->lines[] = "outro";
} catch (Error $e) {
echo get_class($e), ": ", $e->getMessage(), "\n";
}
Appending to $doc->lines fails with Cannot indirectly modify readonly property Document::$lines, because the append would change the array held by the property. The tag added through $doc->meta->tags succeeds: the property holds an object identifier, and the object's own properties are not readonly. Replacing the object itself, as the last block tries, is rejected. Deep immutability therefore needs readonly properties in the nested class as well.
$ php read_nested.php draft Error: Cannot indirectly modify readonly property Document::$lines
Invalid declarations are compile errors
Two mistakes are caught before the script runs: a readonly property with a default value and a readonly property without a type.
<?php
declare(strict_types=1);
class Config
{
public readonly string $environment = "production";
}
PHP reports Readonly property Config::$environment cannot have default value. Removing the value and assigning the property in the constructor fixes the class. Dropping the type instead produces Readonly property Config::$environment must have type, because the engine has to know the type of a value that can never be reassigned.
$ php read_invalid.php
Fatal error: Readonly property Config::$environment cannot have default value in /app/read_invalid.php on line 7
Stack trace:
#0 {main}
Best practices
Use readonly where the value is part of the identity of the object and never changes during its lifetime.
- Declare whole classes
readonlyfor value objects, DTOs, and configuration snapshots. - Combine readonly with constructor promotion to keep the declaration short.
- Do not expect deep immutability; make the nested objects readonly too if they must not change.
- Replace a changed object with a new instance instead of adding a setter.
- Remember that readonly properties need a type and cannot have a default value.
The related constructor property promotions tutorial explains the promoted syntax in more detail, and PHP properties covers typed properties in general.
Source
PHP manual: readonly properties
PHP manual: constructor promotion
This article covered the PHP readonly keyword: single readonly properties, the error raised by assignment after construction, promoted readonly parameters, readonly classes, inheritance, and the limits of the guarantee. Continue with PHP public keyword for visibility, or read constructor property promotions for the short declaration syntax.
Author
List all PHP tutorials.