Core\Reflect\AttributeInfo::target
$attributeInfo->target(): stringReturns the name of the property or method that an attribute is written on.
An attribute can be written on a class, on a property, on a method or on a parameter of a method. For a property, target returns its name without the $. For a method, it returns the name of the method. For an attribute on the class or interface itself, it returns an empty string.
An attribute on a parameter has the name of its method as target. The parameter method returns the name of the parameter.
Use target to find the attributes of one property, or to connect each property with a setting such as the name of a database column.
Return value
Returns string. A property's name without the $, or a method's name. For the class or interface declaration itself, it is the empty string. For an attribute on a parameter, this is the method's name, and parameter returns the parameter's name.
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
Where each attribute is written
<?nvs// Shows which part of a class each attribute is written on.
attribute Table on class { name: string }attribute Column on property { name: string }attribute Cache on method { seconds: int }
#[Table(name: 'products')]class Product{ #[Column(name: 'product_name')] public string $name = '';
#[Cache(seconds: 300)] public function price(): int { return 0; }}
var $info = Core\Reflect::forObject(new Product());
// `target` returns the property or method name, without a `$`. For the class itself, it// returns an empty string, so this prints `<class>`, then `name`, then `price`.foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { echo $attribute->target() == '' ? '<class>' : $attribute->target(), "\n";}<class>namepriceThe attributes of one property
<?nvs// Shows how to find the attributes written on one property.
// `repeatable` allows more than one `Rule` on the same property.repeatable attribute Rule on property { required?: bool, maxLength?: int }
class Signup{ #[Rule(required: true)] #[Rule(maxLength: 254)] public string $email = '';
#[Rule(maxLength: 40)] public string $nickname = '';}
var $info = Core\Reflect::forObject(new Signup());
// Two attributes are written on `email`, so this prints two lines:// `required=1` and `maxLength=254`.foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { if ($attribute->target() != 'email') { continue; } foreach ($attribute->fields() as string $field) { echo $field, '=', $attribute->field($field) as string, "\n"; }}required=1maxLength=254Column names for an insert
<?nvs// Shows how to build an SQL `INSERT` statement from the column attributes on the properties of a class.
attribute Column on property { name: string }
class Customer{ #[Column(name: 'full_name')] public string $fullName = '';
#[Column(name: 'email_address')] public string $email = '';
// This property has no `Column` attribute, so it is not saved. public bool $isLoggedIn = false;}
class Insert{ public static function sql(string $table, string $className): string { array<string> $columns = []; array<string> $placeholders = []; var $info = Core\Reflect::forClass($className); if ($info == null) { return ''; } foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { // `target` is the property the attribute is written on. // The class declaration itself has an empty `target`, so it is skipped. if ($attribute->name() != 'Column' || $attribute->target() == '') { continue; } // The column name is written in this class, so the program can trust it. $columns[] = Core\Taint::assertTrusted( $attribute->field('name') as string, 'the column name is written in a `Column` attribute of this class', ); $placeholders[] = ':' . $attribute->target(); } return 'INSERT INTO ' . $table . ' (' . Core\Str::join($columns, ', ') . ') VALUES (' . Core\Str::join($placeholders, ', ') . ')'; }}
// This prints `INSERT INTO customers (full_name, email_address) VALUES (:fullName, :email)`.echo Insert::sql('customers', 'Customer'), "\n";INSERT INTO customers (full_name, email_address) VALUES (:fullName, :email)