Skip to content

Core\Reflect\ClassInfo::readableProperties

$classInfo->readableProperties(): array<string>

Returns the names of the properties that your code can read at the place where it calls this method.

readableProperties() returns an array of strings. Outside the class, the list has only the public properties. Inside the class, the list has every property of the class. Inside a child class, the list also has the protected properties. These are the same rules as for a normal property read, so get can read every name in the list at the same place.

The answer depends on where you call it, so the same description can return two different lists. properties lists every property of the class, wherever you call it.

The examples below compare the list outside and inside a class, show what a child class can read, and write a debug line with the readable values of an object.

Return value

Returns array<string>. One name for each property that code here may read, without the $. Inside the described class, that is every declared property. Anywhere else, it is the public ones. properties returns the complete list. get throws an error here for exactly the properties that are in that list and not in this one.

Examples

Outside and inside a class

<?nvs
// Shows that the list is different outside and inside the class.
class Session
{
public string $user = 'ada';
private string $token = 'x9f2';
public function fields(): array<string>
{
// Inside the class, both properties can be read.
var $info = Core\Reflect::forObject($this);
return $info->readableProperties();
}
}
Session $session = new Session();
// Outside the class, only the public property is in the list. This prints `user`.
var $info = Core\Reflect::forObject($session);
foreach ($info->readableProperties() as string $name) {
echo 'outside: ', $name, "\n";
}
// This prints `user` and `token`.
foreach ($session->fields() as string $name) {
echo 'inside: ', $name, "\n";
}
Output
outside: user
inside: user
inside: token

What a child class can read

<?nvs
// Shows that code in a child class can read the protected properties of its parent.
class Entry
{
public string $title = 'Hello';
protected int $views = 7;
private string $draft = 'notes';
}
class Post extends Entry
{
public function fields(): array<string>
{
var $found = Core\Reflect::forClass('Entry');
if ($found == null) {
return [];
}
return $found->readableProperties();
}
}
// This prints `title` and `views`. The private property `draft` is not in the list,
// because only code in `Entry` can read it.
foreach ((new Post())->fields() as string $name) {
echo $name, "\n";
}
Output
title
views

Write a debug line for an object

<?nvs
// Shows how a debug helper prints the readable values of an object and leaves out private data.
class Order
{
public string $id = 'A-17';
public string $status = 'paid';
private string $cardNumber = '4111 1111 1111 1111';
}
class Debug
{
public static function line(mixed $object): tainted string
{
var $info = Core\Reflect::forObject($object);
tainted string $text = $info->name() . ' {';
// `get` can read every name in the list, because the list follows the same rules.
foreach ($info->readableProperties() as string $name) {
$text = $text . ' ' . $name . '=' . ($info->get($object, $name) as string);
}
return $text . ' }';
}
}
// This prints `Order { id=A-17 status=paid }`. The card number is private, so it is not printed.
echo Debug::line(new Order()), "\n";
Output
Order { id=A-17 status=paid }