Skip to content

Core\Reflect\PropertyInfo::isReadonly

$propertyInfo->isReadonly(): bool

Tells you whether a property is declared readonly.

isReadonly() returns true for a property declared readonly. It returns false for every other property. A readonly property that a class inherits from its parent class returns true too.

The constructor sets a readonly property, and no other code can change it. After the constructor, Core\Reflect\ClassInfo::set throws a RuntimeError for it. Checking isReadonly() first lets your program skip those properties.

The examples below print which properties of a class are readonly, show a readonly property from a parent class, and write new values into an object without its readonly properties.

Return value

Returns bool. true for a property declared readonly, also when a parent class declares it. After the constructor sets a readonly property, Core\Reflect\ClassInfo::set on it throws a RuntimeError.

Changelog

VersionChange
0.0.6Core\Reflect\PropertyInfo::isLateInit returns true for a property declared lateinit. Core\Reflect\PropertyInfo::isReadonly returns true for a property declared readonly. Both also work for a property that the class inherits.

Examples

Show which properties are readonly

<?nvs
// Shows which properties of a class are declared `readonly`.
class Invoice
{
public string $status = 'open';
public function constructor(public readonly int $number, public readonly int $cents)
{}
}
// This prints `status: no`, `number: yes` and `cents: yes`.
foreach (Core\Reflect::forObject(new Invoice(1001, 4500))->properties() as Core\Reflect\PropertyInfo $property) {
echo $property->name(), ': ', $property->isReadonly() ? 'yes' : 'no', "\n";
}
Output
status: no
number: yes
cents: yes

A readonly property from the parent class

<?nvs
// Shows that `isReadonly` also returns `true` for a `readonly` property that a child class inherits.
class Entity
{
public function constructor(public readonly int $id)
{}
}
class Customer extends Entity
{
public string $name = 'Ada';
}
// `Customer` extends `Entity`, so it inherits the `id` property of `Entity`.
// This prints `id: yes` and `name: no`.
foreach (Core\Reflect::forObject(new Customer(7))->properties() as Core\Reflect\PropertyInfo $property) {
echo $property->name(), ': ', $property->isReadonly() ? 'yes' : 'no', "\n";
}
Output
id: yes
name: no

Apply changes and skip readonly properties

<?nvs
// Shows a function that writes new values into an object and skips each `readonly` property.
class Product
{
public string $name = 'Lamp';
public int $price = 30;
public function constructor(public readonly int $id)
{}
}
class Patch
{
public static function apply(mixed $object, array<string|int> $changes): void
{
var $info = Core\Reflect::forObject($object);
foreach ($info->properties() as Core\Reflect\PropertyInfo $property) {
// `Core\Reflect\ClassInfo::set` writes a property. It throws a `RuntimeError`
// for a `readonly` property, so the loop skips it.
if ($property->isReadonly() || !Core\Arr::hasKey($changes, $property->name())) {
continue;
}
$info->set($object, $property->name(), $changes[$property->name()]);
}
}
}
Product $lamp = new Product(12);
Patch::apply($lamp, ['id' => 99, 'price' => 25]);
// The price changes and the id stays the same. This prints `12 Lamp 25`.
echo $lamp->id, ' ', $lamp->name, ' ', $lamp->price, "\n";
Output
12 Lamp 25