Core\Reflect\ClassInfo::get
$classInfo->get(mixed $object, string $name): mixed$classInfo->get( mixed $object, string $name, ): mixed
Reads a property of an object when the property's name is in a string.
get($object, $name) returns the value of the property $name on $object. The result has the type mixed, so convert it with as. If the property has a get hook, the hook runs, the same as for a normal read.
get follows the same rules as a normal property read at the same place in your code. A private or protected property cannot be read from outside its class, and get throws a RuntimeError. A name that is not a property of the class throws a LogicError. You cannot switch the visibility check off.
The examples below show a read by name, the two errors, and a report that writes only the columns a user chose.
Parameters
$objectmixedAn instance of the described class. The description is of a class and does not contain an object, so you pass the object to read here.$namestringThe property's name without the$, aspropertiesreturns it. The text cannot betainted. Check text from a request withCore\Taint::assertTrustedfirst.
Return value
Returns mixed. The property's value. Its type is mixed, whatever type the property declares.
Errors
RuntimeError$objectis not an object, or$nameis a property that is notpublic. Reflection has the same visibility rules as ordinary code. Ordinary code outside the class gets the same error for this read.LogicError$objectis not an instance of the described class, or the class has no property called$name. Both are mistakes in the program, not facts about the value. That is how they differ from theRuntimeErrorabove.
Examples
Read a property by name
<?nvs// Shows how to read a property when its name is in a string.
class Book{ public string $title = 'The Hobbit'; public int $pages = 310;}
var $book = new Book();var $info = Core\Reflect::forObject($book);
// `get` reads the property "title" of `$book`. This prints "The Hobbit".echo $info->get($book, 'title') as string, "\n";
// The result has the type `mixed`. `as int` converts it back. This prints 320.echo ($info->get($book, 'pages') as int) + 10, "\n";The Hobbit320A private property cannot be read
<?nvs// Shows the two errors `get` throws for a property you cannot read.
class Account{ public string $email = 'ana@example.com'; private string $password = 'secret';}
var $account = new Account();var $info = Core\Reflect::forObject($account);
// `$password` is private. Code outside the class cannot read it, so `get`// throws a `RuntimeError`. This prints "private".try { echo $info->get($account, 'password') as string, "\n";} catch (RuntimeError $error) { echo 'private', "\n";}
// The class has no property "emial". A wrong name throws a `LogicError`.// This prints "no such property".try { echo $info->get($account, 'emial') as string, "\n";} catch (LogicError $wrongName) { echo 'no such property', "\n";}privateno such propertyExport the columns a user chose
<?nvs// Shows how a report writes only the columns a user chose, one line per order.
class Order{ public function constructor( public int $id, public string $customer, public string $total, ) {}}
array<Order> $orders = [new Order(1042, 'Ana', '19.90'), new Order(1043, 'Ben', '5.00')];
// These are the column names the user chose, for example in a form.array<string> $columns = ['id', 'total'];
// `get` reads each chosen property. This prints "id,total", then "1042,19.90" and "1043,5.00".echo Core\Str::join($columns, ','), "\n";foreach ($orders as Order $order) { var $info = Core\Reflect::forObject($order); array<tainted string> $cells = []; foreach ($columns as string $column) { $cells[] = $info->get($order, $column) as string; } echo Core\Str::join($cells, ','), "\n";}id,total1042,19.901043,5.00