Core\Reflect\AttributeInfo::parameter
$attributeInfo->parameter(): stringReturns the name of the method parameter that an attribute is written on.
The name has no $. For an attribute on a class, a property or a method, the result is an empty string.
Use parameter together with target. target gives the method, and parameter gives the parameter of that method. An attribute on a method and an attribute on one of its parameters have the same target. Only the second one has a parameter that is not empty.
A web framework can use this to find out where each argument of a handler method comes from, for example from the path or from the request body.
Return value
Returns string. The parameter's name without the $. For an attribute that is not on a parameter, it is the empty string. With target, you can tell #[X] public function f(...) from public function f(#[X] int $n). These two attributes are on different declarations.
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
A method or its parameter
<?nvs// Shows how to tell an attribute on a method from an attribute on one of its parameters.
attribute Path on method { path: string }attribute From on parameter { source: string }
class InvoiceController{ #[Path(path: '/invoices/{id}')] public function show(#[From(source: 'path')] int $id): int { return $id; }}
var $info = Core\Reflect::forObject(new InvoiceController());
// Both attributes have the target `show`, but only the second one is written on a parameter.// This prints `show()` and then `show($id)`.foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { if ($attribute->parameter() == '') { echo $attribute->target(), "()\n"; } else { echo $attribute->target(), '($', $attribute->parameter(), ")\n"; }}show()show($id)Empty everywhere else
<?nvs// Shows that `parameter` is an empty string for an attribute on a class, a property or a method.
attribute Table on class { name: string }attribute Column on property { name: string }attribute Cache on method { seconds: int }
#[Table(name: 'notes')]class Note{ #[Column(name: 'body')] public string $text = '';
#[Cache(seconds: 60)] public function summary(): string { return ''; }}
var $info = Core\Reflect::forObject(new Note());
// None of the three attributes is written on a parameter.// This prints `0` three times, the length of each empty string.foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { echo Core\Str::length($attribute->parameter()), "\n";}000Where each argument comes from
<?nvs// Shows how a web framework finds out where each argument of a handler method comes from.
attribute From on parameter { source: string }
class ArticleController{ public function update( #[From(source: 'path')] int $id, #[From(source: 'body')] string $title, #[From(source: 'header')] string $authorization, ): void {}}
class Binding{ public static function describe(string $className, string $method): array<tainted string> { array<tainted string> $lines = []; var $info = Core\Reflect::forClass($className); if ($info == null) { return $lines; } foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { if ($attribute->target() != $method || $attribute->parameter() == '') { continue; } $lines[] = '$' . $attribute->parameter() . ' is read from the ' . $attribute->field('source') as string; } return $lines; }}
// This prints one line for each parameter of `update`, in the order they are written.foreach (Binding::describe('ArticleController', 'update') as tainted string $line) { echo $line, "\n";}$id is read from the path$title is read from the body$authorization is read from the header