Skip to content

Core\Reflect\ClassInfo::parent

$classInfo->parent(): ?string

Returns the name of the class that the described class extends.

parent() returns the full name of the parent class, written the same way name() writes a name. It returns null for a class that does not extend another class. It also returns null for an interface, because an interface has no parent class.

attributes() lists only the attributes written in one class. To read the attributes of every base class too, start at a class and call Core\Reflect::forClass($info->parent()) in a loop. The loop stops when parent() returns null. forClass also returns null for a Core class, so the loop stops there too.

Good to know: parent() returns a name, not a description. Call Core\Reflect::forClass with the name when you need the description of the parent class.

The examples below print the parent of a class, walk from a class up to its root class, and read the attributes of every base class.

Return value

Returns ?string. The parent class's name, with its namespace, written as name writes a name. It is null for a class that extends no class. It is also null for an interface. To walk up to the top class, pass the name to Core\Reflect::forClass and call parent again. forClass returns null for a Core class, so the walk stops there too.

Changelog

VersionChange
0.0.5Core\Reflect\ClassInfo::parent returns the name of the class this class extends. It returns null for an interface and for a class that extends nothing. Call it in a loop to read the attributes of each parent class.
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

Print the parent of a class

<?nvs
// Shows the name `parent()` returns for a class, and the `null` it returns when there is no parent.
namespace Shop;
class Product
{
}
class Book extends Product
{
}
interface Priced
{
}
// `Book` extends `Product`, so this prints "Shop\Product".
echo Core\Reflect::forObject(new Book())->parent() ?? 'none', "\n";
// `Product` extends no class, so `parent()` returns `null`. This prints "none".
echo Core\Reflect::forObject(new Product())->parent() ?? 'none', "\n";
// An interface has no parent class. This prints "none" too.
echo Core\Reflect::forClass('Shop\\Priced')?->parent() ?? 'none', "\n";
Output
Shop\Product
none
none

Walk up to the root

<?nvs
// Walks from a class up through every class it extends, and prints each name.
abstract class Animal
{
}
class Bird extends Animal
{
}
class Parrot extends Bird
{
}
// Each round describes the next parent class with `forClass`.
// The loop stops when `parent()` returns `null`. This prints Parrot, Bird and Animal.
?Core\Reflect\ClassInfo $level = Core\Reflect::forObject(new Parrot());
while ($level != null) {
echo $level->name(), "\n";
?string $parent = $level->parent();
$level = $parent == null ? null : Core\Reflect::forClass($parent);
}
Output
Parrot
Bird
Animal

Read the attributes of every base class

<?nvs
// Prints the columns of a database table from a class and from every class it extends.
attribute Column on property { name: string }
abstract class Record
{
#[Column(name: 'id')]
public int $id = 0;
}
class Document extends Record
{
#[Column(name: 'title')]
public string $title = '';
}
class Invoice extends Document
{
#[Column(name: 'total')]
public int $total = 0;
}
// This loop reads the attributes of one class, then moves to its parent class.
// `Invoice` comes first, so this prints `total`, `title` and `id`.
?Core\Reflect\ClassInfo $level = Core\Reflect::forObject(new Invoice());
while ($level != null) {
foreach ($level->attributes() as Core\Reflect\AttributeInfo $attribute) {
if ($attribute->name() == 'Column') {
echo $attribute->field('name') as string, "\n";
}
}
?string $parent = $level->parent();
$level = $parent == null ? null : Core\Reflect::forClass($parent);
}
Output
total
title
id