Core\Reflect\PropertyInfo::isLateInit
$propertyInfo->isLateInit(): boolTells you whether a property is declared lateinit.
isLateInit() returns true for a property declared lateinit. It returns false for every other property. A lateinit property that a class inherits from its parent class returns true too.
A lateinit property has no default value, and the constructor does not have to set it. Your program sets it later. Until then, a read of the property throws a LogicError. Checking isLateInit() first lets your program skip those properties.
The examples below print which properties of a class are lateinit, show a lateinit property from a parent class, and save an object as text without its lateinit properties.
Return value
Returns bool. true for a property declared lateinit, also when a parent class declares it. A lateinit property may stay unset after the constructor. Use isinit to check if it has a value.
Changelog
| Version | Change |
|---|---|
0.0.6 | Core\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 lateinit
<?nvs// Shows which properties of a class are declared `lateinit`.
class Order{ public int $id = 42; public lateinit string $invoiceNumber; public lateinit string $trackingCode;}
// This prints `id: no`, `invoiceNumber: yes` and `trackingCode: yes`.foreach (Core\Reflect::forObject(new Order())->properties() as Core\Reflect\PropertyInfo $property) { echo $property->name(), ': ', $property->isLateInit() ? 'yes' : 'no', "\n";}id: noinvoiceNumber: yestrackingCode: yesA lateinit property from the parent class
<?nvs// Shows that `isLateInit` also returns `true` for a `lateinit` property that a child class inherits.
class Page{ public string $title = 'Home'; public lateinit string $html;}
class BlogPage extends Page{ public string $author = 'Ada';}
// `BlogPage` extends `Page`, so it inherits the properties of `Page`.// This prints `title: no`, `html: yes` and `author: no`.foreach (Core\Reflect::forObject(new BlogPage())->properties() as Core\Reflect\PropertyInfo $property) { echo $property->name(), ': ', $property->isLateInit() ? 'yes' : 'no', "\n";}title: nohtml: yesauthor: noSave an object without its lateinit properties
<?nvs// Shows a function that saves the properties of an object as text and skips each `lateinit` property.
class Cart{ public int $items = 3; public string $currency = 'EUR'; public lateinit int $shippingCost;}
class Saver{ public static function save(mixed $object): tainted string { var $info = Core\Reflect::forObject($object); tainted string $text = ''; foreach ($info->properties() as Core\Reflect\PropertyInfo $property) { // A `lateinit` property may have no value yet. Reading it then throws a `LogicError`. if ($property->isLateInit()) { continue; } $text = $text . $property->name() . '=' . $info->get($object, $property->name()) as string . "\n"; } return $text; }}
// The cart has no shipping cost yet. This prints `items=3` and `currency=EUR`.echo Saver::save(new Cart());items=3currency=EUR