Reflection
Reflection is a set of classes that inspect a program while it runs. A reflection object can report the name, methods and properties of a class, and it can read the attributes stored on them.
A ReflectionClass reports a name and methods
A ReflectionClass is built from the Book class name. The name prints, and two hasMethod verdicts show one method present and one method missing.
<?php
class Book {
public function title() {
return "Dune";
}
}
$ref = new ReflectionClass(Book::class);
echo $ref->getName() . "\n";
var_dump($ref->hasMethod("title"));
var_dump($ref->hasMethod("missing"));A property attribute read back at last
The Label attribute sits on the title property, as attributes did in the earlier lesson. Reflection finds the attribute on the property, builds it, and the stored text prints.
<?php
#[Attribute]
class Label {
public function __construct(public string $text) {
}
}
class Book {
#[Label("A fine book")]
public string $title = "Dune";
}
$ref = new ReflectionClass(Book::class);
$prop = $ref->getProperty("title");
$attrs = $prop->getAttributes(Label::class);
$label = $attrs[0]->newInstance();
echo $label->text . "\n";Method names in declaration order
The getMethods method returns every method of the Shelf class. The names print in the order the class declares them.
<?php
class Shelf {
public function add() {
}
public function clear() {
}
public function count() {
return 0;
}
}
$ref = new ReflectionClass(Shelf::class);
$names = [];
foreach ($ref->getMethods() as $method) {
$names[] = $method->getName();
}
echo implode(", ", $names) . "\n";A class level attribute read the same way
This Label sits on the class itself instead of a property. The getAttributes call on the ReflectionClass finds it, and the built attribute prints its text.
<?php
#[Attribute]
class Label {
public function __construct(public string $text) {
}
}
#[Label("Rare books")]
class Shelf {
}
$ref = new ReflectionClass(Shelf::class);
$attrs = $ref->getAttributes(Label::class);
$label = $attrs[0]->newInstance();
echo $label->text . "\n";Example
<?php
#[Attribute]
class Label {
public function __construct(public string $text) {
}
}
class Novel {
#[Label("Story")]
public string $title = "Dune";
public string $author = "Herbert";
}
$ref = new ReflectionClass(Novel::class);
echo $ref->getName() . "\n";
echo count($ref->getProperties()) . "\n";
$prop = $ref->getProperty("title");
$label = $prop->getAttributes(Label::class)[0]->newInstance();
echo $label->text . "\n";Key points
- A ReflectionClass object describes one class.
- The getName method reports the name of the class.
- The hasMethod method answers whether a class has a named method.
- The getProperty method hands back one property for closer inspection.
- The getAttributes method returns the attributes placed on a class or a property.
- The newInstance method on an attribute builds the attribute object so its data can be read.
In this example, one ReflectionClass describes the Novel class. The name prints first, and a count of the properties prints next. The Label on the title property is then built through reflection, and its text prints last.
Note
Reflection reads the shape of a class at run time, so the answers come from the class itself and not from a separate list.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
What does the newInstance method on an attribute return?