Core\Reflect\AttributeInfo::name
$attributeInfo->name(): stringReturns the full name of the declaration an attribute was written with.
Every attribute has a declaration, such as attribute Route on class { path: string }. The name includes the namespace. In the namespace Shop, #[Route(path: "/orders")] gives Shop\Route. An attribute declared inside a class also has the class name, such as Shop\Controller::Cache. An attribute declared outside any namespace has its short name, such as Route.
Core\Reflect gives you one Core\Reflect\AttributeInfo for each attribute of a class. Compare name with the name you are looking for to keep one kind of attribute and skip all the others.
Good to know: two declarations in different namespaces can have the same short name. The full name tells them apart.
Return value
Returns string. The full name of the declaration, with its namespace. The name is the same for #[Index] and #[Storable::Index], because both are the same attribute.
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
The full name
<?nvs// Shows the full name of each attribute on a class, with its namespace.
namespace Shop;
attribute Route on class { path: string }
abstract class Controller{ attribute Cache on class { seconds: int }}
#[Route(path: '/orders')]#[Cache(seconds: 60)]class OrderController extends Controller{}
var $info = Core\Reflect::forObject(new OrderController());
// `Cache` is declared inside the class `Controller`, so its name includes the class.// This prints `[Shop\Route]` and then `[Shop\Controller::Cache]`.foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { echo '[', $attribute->name(), "]\n";}[Shop\Route][Shop\Controller::Cache]Only one kind of attribute
<?nvs// Shows how to keep only the attributes with one name and skip all the others.
attribute Deprecated on method { since: string }attribute Cache on method { seconds: int }
class Api{ #[Deprecated(since: '2.0')] public function oldSearch(): void {}
#[Cache(seconds: 60)] public function search(): void {}
#[Deprecated(since: '2.4')] public function oldExport(): void {}}
var $info = Core\Reflect::forObject(new Api());
// `name` is the name of the attribute's declaration. This prints// `oldSearch` and `oldExport`, the two methods that have `Deprecated`.foreach ($info->attributes() as Core\Reflect\AttributeInfo $attribute) { if ($attribute->name() == 'Deprecated') { echo $attribute->target(), "\n"; }}oldSearcholdExportA route table from a controller
<?nvs// Shows how a small web framework builds its route table from the attributes of a controller class.
attribute Route on method { method: string, path: string }attribute Internal on method { reason: string }
class UserController{ #[Route(method: 'GET', path: '/users')] public function list(): void {}
#[Route(method: 'POST', path: '/users')] public function create(): void {}
// This attribute is not a route, so the table does not include it. #[Internal(reason: 'only the build script calls it')] public function rebuildCache(): void {}}
class Routes{ public static function of(string $className): 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->name() != 'Route') { continue; } tainted string $method = $attribute->field('method') as string; tainted string $path = $attribute->field('path') as string; $lines[] = $method . ' ' . $path . ' -> ' . $className . '::' . $attribute->target(); } return $lines; }}
// This prints one line for each route: the HTTP method, the path and the method that handles it.foreach (Routes::of('UserController') as tainted string $line) { echo $line, "\n";}GET /users -> UserController::listPOST /users -> UserController::create