Skip to content

Core\Program::implementingWith

Core\Program::implementingWith<I, T>(
    string $member = "",
): array<{ instance: I, attribute: ?T }>

Returns every class that implements an interface or extends a class, each next to the one attribute you ask for.

Core\Program::implementing finds the classes. This method finds the same classes and reads one attribute of each of them, so a framework can find its pages and their routes in one call. You write the interface or the class, and the name of an attribute declaration, between < and >. The argument is the name of the method, property or constructor parameter that has the attribute. An empty name reads the attributes of the class itself. Each row has two fields: instance is the object, and attribute is the matching attribute, or null when the class has none.

Good to know: Novis resolves this while it compiles your program, so nothing is scanned while the program runs. Two matching attributes on one class do not compile.

The examples below build a dispatch table from pages and their routes, order plugins by an attribute on the class, read a setting from a property, and find scheduled jobs that extend a base class.

Parameters

  • $memberstringdefault ""The name of the method, property or constructor parameter that has the attribute. An empty string reads the attributes of the class itself.

Return value

Returns array<{ instance: I, attribute: ?T }>. An array with one row per class, sorted by class name. Each row is { instance: I, attribute: ?T }. attribute is null when the class has no attribute of the shape T. Two matching attributes on one class do not compile.

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

Pages and their routes

<?nvs
// Shows how a framework finds every page and the path of its route in one
// call. A page writes its path once, in the attribute.
interface Page
{
// Every class that implements `Page` may use this attribute.
attribute PagePath on method { path: string }
public function render(): string;
}
class Home implements Page
{
#[PagePath(path: '/')]
public function render(): string
{
return 'Welcome';
}
}
class Contact implements Page
{
#[PagePath(path: '/contact')]
public function render(): string
{
return 'Write to us';
}
}
// This call returns every page with the `PagePath` attribute of its `render` method.
// The rows are sorted by class name.
array<Page> $pages = [];
foreach (Core\Program::implementingWith<Page, Page::PagePath>('render') as var $row) {
var $route = $row->attribute;
if ($route != null) {
$pages[$route->path] = $row->instance;
}
}
// This prints `Write to us`, then `Welcome`.
echo $pages['/contact']->render(), "\n";
echo $pages['/']->render(), "\n";
Output
Write to us
Welcome

Plugins ordered by a class attribute

<?nvs
// Shows how to read an attribute that is on the class itself: the member name
// is left out. A class without the attribute gets `null`, so a default is easy.
attribute Order on class { priority: int }
interface Plugin
{
public function name(): string;
}
#[Order(priority: 10)]
class Cache implements Plugin
{
public function name(): string
{
return 'cache';
}
}
class Logger implements Plugin
{
public function name(): string
{
return 'logger';
}
}
#[Order(priority: 1)]
class Auth implements Plugin
{
public function name(): string
{
return 'auth';
}
}
// Each plugin with its priority, and 100 for a plugin with no `Order` attribute.
// The rows come in class-name order, so this prints `auth 1`, `cache 10`, `logger 100`.
foreach (Core\Program::implementingWith<Plugin, Order>() as var $row) {
echo $row->instance->name(), ' ', $row->attribute?->priority ?? 100, "\n";
}
Output
auth 1
cache 10
logger 100

A setting read off a property

<?nvs
// Shows how to read an attribute that is on a property: the argument is the
// property's name. A constructor parameter with `public` counts as a property.
attribute Unit on property { unit: string }
interface Sensor
{
public function reading(): float;
}
class Thermometer implements Sensor
{
#[Unit(unit: '°C')]
public float $value = 21.5;
public function reading(): float
{
return $this->value;
}
}
class Barometer implements Sensor
{
public function constructor(#[Unit(unit: 'hPa')] public float $value = 1013.0)
{}
public function reading(): float
{
return $this->value;
}
}
// Each sensor with the unit its `$value` property declares.
// This prints `1013 hPa` and `21.5 °C`.
foreach (Core\Program::implementingWith<Sensor, Unit>('value') as var $row) {
echo $row->instance->reading(), ' ', $row->attribute?->unit ?? '?', "\n";
}
Output
1013 hPa
21.5 °C

Jobs that extend a base class

<?nvs
// Shows how to find every class that extends a base class, with an attribute that says how often it runs.
abstract class Job
{
// Every class that extends `Job` may use this attribute.
attribute Schedule on class { every: string }
abstract public function run(): string;
}
#[Schedule(every: 'hour')]
class CleanUp extends Job
{
public function run(): string
{
return 'old files removed';
}
}
#[Schedule(every: 'day')]
class Invoices extends Job
{
public function run(): string
{
return 'invoices sent';
}
}
class Manual extends Job
{
public function run(): string
{
return 'nothing to do';
}
}
// `Manual` has no `Schedule` attribute, so its `attribute` is `null`.
// This prints `hour`, `day` and `manual only` at the start of the three lines.
foreach (Core\Program::implementingWith<Job, Job::Schedule>() as var $row) {
echo $row->attribute?->every ?? 'manual only', ': ', $row->instance->run(), "\n";
}
Output
hour: old files removed
day: invoices sent
manual only: nothing to do