Declare a Readonly PHP Class Without Invalid Properties
Build a PHP 8.2 readonly data holder, then audit invalid properties, class relationships and writes to its objects.
To make a small data holder readonly, use readonly class on PHP 8.2 or later, but check the existing class before adding the modifier. Its properties need suitable declarations, its class relationships must be compatible, and callers must not depend on reassigning initialized properties or adding undeclared ones. The practical choice is whether the object should retain the values supplied when it is constructed.
Declare and read a typed data holder
Readonly classes are available as of PHP 8.2. Check the version of the PHP interpreter you will use for this example:
php -v
Proceed if it reports PHP 8.2 or later. In this self-contained example, the constructor parameters declare two typed properties and supply their initial values:
<?php
readonly class ReceiptLine
{
public function __construct(
public string $label,
public int $quantity,
) {}
}
$line = new ReceiptLine('Notebook', 2);
echo $line->label, "\n";
echo $line->quantity, "\n";
Save only that code as receipt-line.php. Lint it, then run it with the interpreter whose version you checked:
php -l receipt-line.php
php receipt-line.php
The two reads show the constructor-supplied values, Notebook and 2. Declaring the class readonly makes its declared properties readonly; there is no need to repeat the modifier on each property. The PHP manual describes that class-level rule. It fits this data holder because its initialized values are meant to be read, not reassigned by callers.
When converting an existing class, do not stop after inserting the modifier. Audit its declarations, any traits it uses, its parent and children, and the code that writes to its objects. Keep declaration problems separate from writes attempted after an object has been constructed.
Check property declarations before adding readonly
Making every declared property readonly does not make every property declaration valid. An untyped property is not permitted. This is a separate invalid example, not a property to add to ReceiptLine:
<?php
readonly class UntypedReceiptLine
{
public $label;
}
Adding readonly to the class does not supply a type for $label. Decide what type the property should hold and declare it before converting the class.
A static property is also not permitted, even when it has a type. This second example has a different declaration problem:
<?php
readonly class StaticReceiptLine
{
public static int $count;
}
Inspect traits as well as properties written directly in the class. The readonly amendments RFC states that the prohibition on untyped and static properties also applies when a trait introduces their declarations. Fixing an untyped declaration in the class will not help if a used trait still introduces one.
Check directly declared property defaults, too. A directly declared readonly property cannot have a default value, as noted in this discussion of readonly properties. If an existing direct property declaration relies on a default, review how that value will be supplied before converting it. The working ReceiptLine example instead receives both values through its constructor.
Check the class relationship and existing writes
If the data holder extends another class, check the parent before adding readonly: a readonly class can extend only a readonly parent. Check children as well, because a non-readonly class cannot extend a readonly class. PHPStan summarizes both inheritance constraints. The standalone ReceiptLine has no such relationship to resolve, but an existing class might.
Now inspect writes against the valid ReceiptLine object. Its constructor has supplied quantity, so this line is a separate attempted reassignment to an initialized property. Do not put it in the runnable example above:
$line->quantity = 3;
That attempt produces an error rather than updating quantity. If a caller needs a different quantity, it cannot obtain one by reassigning this property on the existing object. Code that expects to revise initialized values therefore needs attention before the class is made readonly.
This next line attempts something different: it names a property that ReceiptLine did not declare. Treat it as another isolated example, not a second operation to run after the reassignment:
$line->note = 'Pack separately';
That write attempts dynamic-property creation, not reassignment of quantity. Readonly classes do not allow dynamic properties. If note belongs in the data holder, decide how to declare and initialize it in the class rather than relying on callers to add it to an object later.
Keep the invalid declarations and attempted writes out of receipt-line.php when checking that file. Its lint and run steps establish that the valid example can be read. Use the isolated examples as a conversion checklist: no untyped or static properties, including those introduced by traits; no incompatible parent or child; and no callers that depend on either kind of prohibited write.
