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
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,
) {}
}