Glossary

readonly (PHP 8.1)

readonly is a PHP 8.1 modifier that allows a property to be written only once (in the constructor) and forbids any subsequent modification. Ideal for Value Objects and DTOs — guarantees immutability without extra code.

Example

class Money {
  public function __construct(
    public readonly int    $amount,
    public readonly string $currency,
  ) {}
}

$price = new Money(100, 'USD');
echo $price->amount;   // 100
$price->amount = 200;  // Error: Cannot modify readonly property

readonly class (PHP 8.2)

PHP 8.2 added readonly class — all properties of the class automatically become readonly. No need to write the modifier for each one.

readonly class Point {
  public function __construct(
    public float $x,
    public float $y,
  ) {}
}