readonly class Point {
public function __construct(
public int $x,
public int $y,
) { }
}
$point= new Point(8, 42);
echo "Hello {$point->x}x{$point->y}!\n";
// Will not modify the member, raising an exception instead:
// $point->x= 9;
[x] Inherit readonly modifiers on classes to all members and promoted constructor parameters
[x] Prevent dynamic members
[ ] Do not allow readonly on traits, interfaces and enums
[ ] Prevent inheritance where readonly modifiers do not match
[ ] Prevent static members in readonly classes
[ ] Disallow #[AllowDynamicProperties] on readonly classes
[ ] Remove emulation for PHP 8.2 once php/php-src#7305 is merged
See https://wiki.php.net/rfc/readonly_classes and xp-framework/ast#37. The following comes quite close to records, although it doesn't declare getters but uses public readonly fields:
readonly
modifiers on classes to all members and promoted constructor parametersreadonly
on traits, interfaces and enumsreadonly
modifiers do not match#[AllowDynamicProperties]
on readonly classes