Skip to content

Core\Attributes::get

Returns the one attribute of one kind attached to a class, a method, a property or a parameter. The result is null when there is none.

You write the attribute's name between < and > at the call. This is the name of an attribute declaration, such as Cache for attribute Cache on method { seconds: int }. Only attributes written as #[Cache(…)] match. Test the result against null before you read a field out of it.

Name the declaration with a method reference, which is the method's name without a call. User::constructor names the class User itself, and User::save names its method save. For a property or a parameter, add its name as a second argument.

Good to know: two matching attributes on one declaration do not compile. The error names Core\Attributes::all as the method that returns both. Novis reads the attributes while it compiles your program, so this costs nothing while the program runs.

The examples below read a length limit off a parameter, then a page size with a default for the class that carries none, then a cache setting a handler may leave out.

Parameters

  • $targetcallableThe class or function whose attributes are read.
  • $memberstringdefault ""The name of a method, property or parameter of $target. Its attributes are read instead of the attributes of $target. The default is the empty string, which reads $target itself.

Return value

Returns ?T. The payload object of the matching attribute, as T. The result is null when no attribute matches T. When more than one matches, the code does not compile, and the error names all<T> as the fix. A payload matches when it has at least the fields of T. So the empty shape {} matches every attribute, and a marker type with no fields gives that compile error when the target has a second attribute.

Changelog

VersionChange
0.0.5Core\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.

Examples

A limit on one parameter

<?nvs
// Shows how to read an attribute that is attached to one parameter. The second
// argument is the name of the parameter.
attribute MaxLength on parameter { max: int }
class Signup
{
public static function register(#[MaxLength(max: 64)] string $email): string
{
return $email;
}
}
// `Signup::register` names the method, and `"email"` picks the parameter out
// of it. This prints 64.
var $limit = Core\Attributes::get<MaxLength>(Signup::register, 'email');
if ($limit != null) {
echo $limit->max, "\n";
}
// The method itself carries no `MaxLength`. Leave the name out and the result is
// `null`, so this prints `the method has no limit`.
var $onTheMethod = Core\Attributes::get<MaxLength>(Signup::register);
echo $onTheMethod == null ? "the method has no limit\n" : "the method has a limit\n";
Output
64
the method has no limit

A default when there is none

<?nvs
// Shows the usual way to use an optional attribute: read it, and use a default
// value when the result is `null`.
attribute PageSize on class { rows: int }
#[PageSize(rows: 100)]
class Invoices
{}
class Customers
{}
class Listing
{
// Returns the number of rows the class asks for, or 25 when it asks for none.
public static function rowsOf(?{ rows: int } $size): int
{
if ($size == null) {
return 25;
}
return $size->rows;
}
}
// `Invoices` carries a `PageSize`, so this prints 100.
echo Listing::rowsOf(Core\Attributes::get<PageSize>(Invoices::constructor)), "\n";
// `Customers` carries none, so the result is `null` and this prints 25.
echo Listing::rowsOf(Core\Attributes::get<PageSize>(Customers::constructor)), "\n";
Output
100
25

How long an answer may be kept

<?nvs
// Shows a setting you can write next to a handler: the handler says how long its
// answer may be reused, and the program reads that setting back.
attribute Cache on method { seconds: int }
class Reports
{
#[Cache(seconds: 300)]
public static function monthly(): string
{
return 'the monthly report';
}
public static function live(): string
{
return 'the live report';
}
}
class Answer
{
public static function send(string $body, ?{ seconds: int } $cache): void
{
echo $body;
if ($cache == null) {
echo " (do not keep)\n";
} else {
echo ' (keep for ', $cache->seconds, " seconds)\n";
}
}
}
// `Reports::monthly` names the method, so this prints
// `the monthly report (keep for 300 seconds)`.
Answer::send(Reports::monthly(), Core\Attributes::get<Cache>(Reports::monthly));
// `live` carries no `Cache`, so the result is `null` and this prints
// `the live report (do not keep)`.
Answer::send(Reports::live(), Core\Attributes::get<Cache>(Reports::live));
Output
the monthly report (keep for 300 seconds)
the live report (do not keep)