Skip to content

Core\Reflect\ClassInfo::properties

$classInfo->properties(): array<Core\Reflect\PropertyInfo>

Returns a list with one description for each property of a class.

properties() returns an array of Core\Reflect\PropertyInfo. Each one has the name of a property, its declared type, and whether it is public. The list has every property the class declares, and private and protected properties are in it too. The order is the order of the declaration, and properties from a parent class come first.

The list only describes the properties. It does not read their values. To read a value, use get, which checks visibility the same way a normal read does. readableProperties returns only the names that your code can read at the place where it calls it.

The examples below list the properties of a class, show that a child class lists the properties of its parent first, and build the header line of a table.

Return value

Returns array<Core\Reflect\PropertyInfo>. One Core\Reflect\PropertyInfo for each declared property. The properties of every parent class come first, then the class's own. A private or protected property is in the list, and its isPublic returns false. get checks whether this code may read it.

Changelog

VersionChange
0.0.6The server now builds the description of each class once, when it loads your program. Core\Reflect::forClass, Core\Reflect::forObject and Core\Reflect\EnumInfo::of return that description, so two calls for the same class return the same object. A list such as properties() or methods() is built once per request.

Examples

List the properties of a class

<?nvs
// Shows how to list the properties of a class with their types.
class Account
{
public string $owner = 'ada';
public ?int $limit = null;
private int $pin = 1234;
}
var $info = Core\Reflect::forObject(new Account());
// Each entry is a `Core\Reflect\PropertyInfo`. This prints three lines.
// The private property `pin` is in the list too.
foreach ($info->properties() as Core\Reflect\PropertyInfo $property) {
string $visibility = $property->isPublic() ? 'public' : 'not public';
echo $property->name(), ': ', $property->type() ?? 'no type', ', ', $visibility, "\n";
}
Output
owner: string, public
limit: ?int, public
pin: int, not public

A child class lists its parents properties first

<?nvs
// Shows that a child class lists the properties of its parent class first.
class Entry
{
public string $title = '';
protected int $views = 0;
}
class Post extends Entry
{
public string $body = '';
}
var $found = Core\Reflect::forClass('Post');
if ($found != null) {
Core\Reflect\ClassInfo $post = $found;
// `Core\Arr::count` gives the number of properties. This prints `3`.
echo Core\Arr::count($post->properties()), "\n";
// This prints `title`, `views` and then `body`.
foreach ($post->properties() as Core\Reflect\PropertyInfo $property) {
echo $property->name(), "\n";
}
}
Output
3
title
views
body

Build the header of a table

<?nvs
// Shows how an export builds the header line of a CSV table from the public properties of a class.
class Product
{
public string $sku = 'A-100';
public string $name = 'Desk lamp';
public int $stock = 12;
private float $cost = 8.5;
}
class Export
{
public static function header(string $className): string
{
var $info = Core\Reflect::forClass($className);
if ($info == null) {
return '';
}
string $line = '';
foreach ($info->properties() as Core\Reflect\PropertyInfo $property) {
// A private property is internal data, so the table does not show it.
if (!$property->isPublic()) {
continue;
}
if ($line != '') {
$line = $line . ',';
}
$line = $line . $property->name();
}
return $line;
}
}
// This prints `sku,name,stock`.
echo Export::header('Product'), "\n";
Output
sku,name,stock