Skip to content
Tutorials Center
My learning

Topics

PHP Reference ยท Language Constructs and Statements

The readonly modifier

Reference for PHP readonly: write-once properties (8.1), readonly classes (8.2), initialization rules, clone behavior and common errors explained.

A readonly property can be initialized once and never changed afterwards. Any second write throws an Error. It was added in PHP 8.1 and is the simplest way to build immutable value objects without writing getters.

A readonly property must have a type, cannot have a default value, and can only be initialized from inside the class scope. readonly class (PHP 8.2) makes every declared property readonly at once.

PHP
final class Point
{
    public function __construct(
        public readonly int $x,
        public readonly int $y,
    ) {}
}

Try it

PHP Write once, then locked

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

PHP Readonly class and arrays inside

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.

Syntax

PHP
public readonly Type $property;
public function __construct(public readonly Type $property) {}
readonly class Name { }

Rules: the property needs a type declaration (use mixed if you do not want one), no default value, and cannot be static. A readonly class can only contain typed non-static properties and cannot have dynamic properties.

Errors: writing twice throws Error ("Cannot modify readonly property ..."). Reading before initialization throws Error ("must not be accessed before initialization"). Initializing from outside the declaring class scope throws Error.

Usage notes

  • Shallow immutability. readonly only fixes the property itself. If the value is an object, the object can still change. Arrays are values, so changing an array property also fails, as the second example shows.
  • Initialization scope. Code outside the class cannot initialize the property, even if it is public. Initialize it in the constructor or a method of the class.
  • clone. A cloned object keeps its readonly values. Changing a readonly property inside __clone() became possible in PHP 8.3. A common pattern is "with" methods that return a new instance.
  • Unset and increment. You cannot unset(), ++ or take a reference to an initialized readonly property.
  • Readonly classes (PHP 8.2) cannot be extended by non-readonly classes. Readonly parent and child must match.
  • Constructor promotion is the usual way to declare them: one line per property.
  • Not a constant. Each object can have different values; use const for fixed class-wide values.
  • PHP 8.4 adds asymmetric visibility (public private(set)), an alternative that allows the class itself to write many times.