Core\Reflect\ClassInfo::attributes
$classInfo->attributes(): array<Core\Reflect\AttributeInfo>Lists every attribute written on a class and on its properties, methods and parameters.
attributes() returns one Core\Reflect\AttributeInfo for each #[...] in the class declaration, in the order they are written. Each one says where it is written and what its fields are. A class with no attributes gives an empty list.
The list only has the attributes written in this class's own declaration. A subclass does not list the attributes of its parent class. To read those, describe the parent class too.
Good to know: if you know the attribute's name when you write the code, Core\Attributes::get is simpler and is checked when the program compiles.
The examples below show where each attribute of a class is written, a subclass that lists only its own attribute, and a form checked against rules written as attributes.
Return value
Returns array<Core\Reflect\AttributeInfo>. One Core\Reflect\AttributeInfo for each place an attribute is written, in source order. The class declaration's attributes come first, then those of each property and method. A method's parameters come after that method. Only the class's own declarations are included. constants also includes the parent classes. An attribute belongs to the declaration it is written on, so a parent class's attributes are not listed. Core\Attributes::get reads an attribute by the shape of its payload. It is checked at compile time and costs nothing at run time. Use attributes for a class whose name is not in your source code. To read the parent classes' attributes too, call parent and describe each parent class with Core\Reflect::forClass.
Changelog
| Version | Change |
|---|---|
0.0.5 | Core\Attributes::get<Table> returns only the attributes written as #[Table(...)], so two attributes with the same fields are not mixed up. For a method that a class inherits and does not override, get and all return the attributes its parent class wrote on it. Core\Reflect\AttributeInfo::name returns the full name of the declaration. |
0.0.6 | The 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
Every attribute of a class
<?nvs// Shows every attribute written on a class and where each one is written.
attribute Route on class, method { path: string }attribute Column on property { name: string }attribute From on parameter { source: string }
#[Route(path: '/users')]class UserController{ #[Column(name: 'user_name')] public string $name = '';
#[Route(path: '/show')] public function show(#[From(source: 'query')] int $id): int { return $id; }}
var $info = Core\Reflect::forObject(new UserController());
// `attributes()` returns one entry for each attribute, in the order they are written.// This prints 4, then one line for each attribute.echo Core\Arr::count($info->attributes()), "\n";foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { // `target()` is empty for an attribute on the class itself. string $where = $attribute->target() == '' ? 'the class' : $attribute->target(); if ($attribute->parameter() != '') { $where = $where . ', parameter $' . $attribute->parameter(); } echo $where, "\n";}4the classnameshowshow, parameter $idA subclass has its own list
<?nvs// Shows that a class only lists the attributes written in its own declaration.
attribute Table on class { name: string }attribute Format on property { format: string }
#[Table(name: 'documents')]class Document{ public string $title = '';}
class Invoice extends Document{ #[Format(format: 'money')] public int $total = 0;}
var $document = Core\Reflect::forObject(new Document());var $invoice = Core\Reflect::forObject(new Invoice());
// `Invoice` extends `Document`, but the `Table` attribute is written on `Document`.// So `Invoice` lists only its own attribute. This prints 1 and then 1.echo Core\Arr::count($document->attributes()), "\n";echo Core\Arr::count($invoice->attributes()), "\n";
// To find the table of an `Invoice`, read the attributes of its parent class.// This prints "documents".foreach ($document->attributes() as Core\Reflect\AttributeInfo $attribute) { if ($attribute->name() == 'Table') { echo $attribute->field('name') as string, "\n"; }}11documentsCheck a form against its rules
<?nvs// Shows how a form library checks submitted values against rules written as// attributes on the properties of a form class.
attribute MaxLength on property { max: int }
class SignupForm{ #[MaxLength(max: 12)] public string $username = '';
#[MaxLength(max: 40)] public string $email = '';
public string $note = '';}
class Validator{ // Returns one error message for each value that is too long. public static function errors(string $formClass, array<string> $input): array<string> { array<string> $errors = []; var $info = Core\Reflect::forClass($formClass); if ($info == null) { return $errors; } foreach ($info->attributes() as Core\Reflect\AttributeInfo $rule) { if ($rule->name() != 'MaxLength') { continue; } string $field = $rule->target(); int $max = $rule->field('max') as int; string $value = $input[$field] ?? ''; if (Core\Str::length($value) > $max) { $errors[] = $field . ' is longer than ' . $max . ' characters'; } } return $errors; }}
array<string> $submitted = [ 'username' => 'a_very_long_username', 'email' => 'ana@example.com', 'note' => 'Please call me after five.',];
// Only `username` is too long. `note` has no rule, so it is not checked.// This prints "username is longer than 12 characters".foreach (Validator::errors('SignupForm', $submitted) as string $error) { echo $error, "\n";}username is longer than 12 characters