Skip to content

Core\Program::implementing

Core\Program::implementing<T>(): array<T>

Returns a new object of every class that implements an interface or extends a class.

You write the interface or the class between < and >, for example Core\Program::implementing<Module>(). The result is an array with one new object per class, sorted by class name. Abstract classes are not in the array. When you write a class that is not abstract, that class is in the array too. Each class needs a constructor without arguments. If your classes need arguments, use Core\Program::constructors. A framework uses this to find its plugins, commands or checks, so nobody has to keep a list of them up to date.

Good to know: Novis finds the classes when it compiles your program, so nothing is searched while the program runs. Every call creates new objects.

The examples below run every health check of a service, pick an export format by its name, print a help page that lists every command, and find every class that extends a base class.

Return value

Returns array<T>. An array<T> with one new object per class, sorted by class name. Every call creates new objects. The array is empty when no class is a T.

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

Run every health check

<?nvs
// Shows how a service runs every health check it has. A new check is a new
// class, and no list has to change.
interface HealthCheck
{
public function name(): string;
public function passes(): bool;
}
class DatabaseCheck implements HealthCheck
{
public function name(): string
{
return 'database';
}
public function passes(): bool
{
return true;
}
}
class DiskCheck implements HealthCheck
{
public function name(): string
{
return 'disk';
}
public function passes(): bool
{
return false;
}
}
// The objects are sorted by class name. This prints `database: ok`, then `disk: failed`.
foreach (Core\Program::implementing<HealthCheck>() as HealthCheck $check) {
echo $check->name(), ': ', $check->passes() ? 'ok' : 'failed', "\n";
}
Output
database: ok
disk: failed

Pick an exporter by name

<?nvs
// Shows how to choose one class by a name that the user types.
interface Exporter
{
public function format(): string;
public function export(array<string> $names): string;
}
class CsvExporter implements Exporter
{
public function format(): string
{
return 'csv';
}
public function export(array<string> $names): string
{
return Core\Str::join($names, ',');
}
}
class LinesExporter implements Exporter
{
public function format(): string
{
return 'lines';
}
public function export(array<string> $names): string
{
return Core\Str::join($names, ' | ');
}
}
// The key of this array is the name of a format, and the value is its exporter.
array<Exporter> $exporters = [];
foreach (Core\Program::implementing<Exporter>() as Exporter $exporter) {
$exporters[$exporter->format()] = $exporter;
}
// This prints `Ada,Grace,Linus`.
echo $exporters['csv']->export(['Ada', 'Grace', 'Linus']), "\n";
// A format that no class has is not in the array. This prints `false`.
echo Core\Arr::hasKey($exporters, 'pdf') ? 'true' : 'false', "\n";
Output
Ada,Grace,Linus
false

Print a help page of commands

<?nvs
// Shows how a command line tool prints a help page that lists every command it has.
interface Command
{
public function name(): string;
public function summary(): string;
}
class Deploy implements Command
{
public function name(): string
{
return 'deploy';
}
public function summary(): string
{
return 'Copies the current version to the servers.';
}
}
class Migrate implements Command
{
public function name(): string
{
return 'migrate';
}
public function summary(): string
{
return 'Updates the database tables.';
}
}
// An abstract class is not in the result, so this base class is not listed.
abstract class HiddenCommand implements Command
{
public function summary(): string
{
return 'Not shown.';
}
}
class Status implements Command
{
public function name(): string
{
return 'status';
}
public function summary(): string
{
return 'Shows what is running.';
}
}
echo "Commands:\n";
foreach (Core\Program::implementing<Command>() as Command $command) {
echo ' ', Core\Str::padEnd($command->name(), 10), $command->summary(), "\n";
}
Output
Commands:
deploy Copies the current version to the servers.
migrate Updates the database tables.
status Shows what is running.

Every class that extends a base class

<?nvs
// Shows how to find every class that extends a base class, not only the classes that implement an interface.
abstract class Discount
{
abstract public function name(): string;
abstract public function percent(): uint;
public function describe(): string
{
return $this->name() . ' takes ' . $this->percent() . '% off.';
}
}
class Holiday extends Discount
{
public function name(): string
{
return 'Holiday';
}
public function percent(): uint
{
return 20;
}
}
class Member extends Discount
{
public function name(): string
{
return 'Member';
}
public function percent(): uint
{
return 10;
}
}
// `Student` extends `Member`, so it is a `Discount` too and is in the list.
class Student extends Member
{
public function name(): string
{
return 'Student';
}
public function percent(): uint
{
return 15;
}
}
// `Discount` is abstract, so it is not in the list. This prints three lines.
foreach (Core\Program::implementing<Discount>() as Discount $discount) {
echo $discount->describe(), "\n";
}
Output
Holiday takes 20% off.
Member takes 10% off.
Student takes 15% off.