Skip to content

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 $, as properties returns it. The text cannot be tainted. Check text from a request with Core\Taint::assertTrusted first.

Return value

Returns mixed. The property's value. Its type is mixed, whatever type the property declares.

Errors

  • RuntimeError$object is not an object, or $name is a property that is not public. Reflection has the same visibility rules as ordinary code. Ordinary code outside the class gets the same error for this read.
  • LogicError$object is 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 the RuntimeError above.

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";
Output
The Hobbit
320

A 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";
}
Output
private
no such property

Export 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";
}
Output
id,total
1042,19.90
1043,5.00