Core\Program::constructors
Core\Program::constructors<T, C>(): array<{ class: string, make: C }>Returns a function for every class that implements an interface, and each function creates an object of its class.
Core\Program::implementing creates the objects itself, so each class needs a constructor without arguments. This member gives you the functions instead, and you pass the arguments when you call one. You write two types between < and >: the interface or class, and the type of the function, for example Core\Program::constructors<Handler, callable(Settings): Handler>(). Each row has two fields: class is the full class name, and make is the function. The rows are sorted by class name, in the same order as implementing.
Good to know: no object is created until you call make. A class whose constructor does not accept these arguments does not compile.
The examples below create one handler by its class name, create only the plugins that are switched on, and print a list of commands without creating any of them.
Return value
Returns array<{ class: string, make: C }>. An array with one row per class, sorted by class name. Each row is { class: string, make: C }. class is the full class name. No object is created until you call make.
Examples
A handler picked by name
<?nvs// Shows how to choose a handler by its class name and create it with the settings it needs.
class Settings{ public function constructor(public string $siteName) {}}
interface Handler{ public function handle(string $user): string;}
class Welcome implements Handler{ public function constructor(private Settings $settings) {}
public function handle(string $user): string { return 'Welcome to ' . $this->settings->siteName . ', ' . $user . '.'; }}
class Goodbye implements Handler{ public function constructor(private Settings $settings) {}
public function handle(string $user): string { return 'Goodbye, ' . $user . '. Come back to ' . $this->settings->siteName . ' soon.'; }}
// The key of this array is the class name, and the value is the function that creates it.array<callable(Settings): Handler> $makers = [];foreach (Core\Program::constructors<Handler, callable(Settings): Handler>() as { class: string, make: callable(Settings): Handler } $row) { $makers[$row->class] = $row->make;}
// Only the handler you choose is created. This prints `Welcome to Shop, Ada.`callable(Settings): Handler $make = $makers['Welcome'];Handler $handler = $make(new Settings('Shop'));echo $handler->handle('Ada'), "\n";Welcome to Shop, Ada.Only the plugins that are switched on
<?nvs// Shows how to create only the plugins that are switched on. A plugin that is// switched off is never created.
interface Plugin{ public function describe(): string;}
class Audit implements Plugin{ public function constructor(private uint $size) { echo "Creating Audit.\n"; }
public function describe(): string { return 'Audit keeps ' . $this->size . ' log lines.'; }}
class Cache implements Plugin{ public function constructor(private uint $size) { echo "Creating Cache.\n"; }
public function describe(): string { return 'Cache keeps ' . $this->size . ' entries.'; }}
class Search implements Plugin{ public function constructor(private uint $size) { echo "Creating Search.\n"; }
public function describe(): string { return 'Search shows ' . $this->size . ' results.'; }}
// The switched-on plugins and their sizes. `Audit` is not in this array.array<uint> $enabled = ['Cache' => 500, 'Search' => 20];
array<Plugin> $plugins = [];foreach (Core\Program::constructors<Plugin, callable(uint): Plugin>() as { class: string, make: callable(uint): Plugin } $row) { if (Core\Arr::hasKey($enabled, $row->class)) { $plugins[] = ($row->make)($enabled[$row->class]); }}
// This prints two lines. `Audit` was never created.foreach ($plugins as Plugin $plugin) { echo $plugin->describe(), "\n";}Creating Cache.Creating Search.Cache keeps 500 entries.Search shows 20 results.A list of commands without creating them
<?nvs// Shows how to list every command by its class name without creating any of them.
class Database{ public function constructor() { echo "Connecting to the database.\n"; }}
interface Command{ public function run(): string;}
class Backup implements Command{ public function constructor(private Database $database) {}
public function run(): string { return 'Backup finished.'; }}
class Migrate implements Command{ public function constructor(private Database $database) {}
public function run(): string { return 'Tables updated.'; }}
class Report implements Command{ public function constructor(private Database $database) {}
public function run(): string { return 'Report sent.'; }}
// This loop does not call `make`. No command is created, so the program never// connects to the database. This prints the three class names.echo "Commands:\n";foreach (Core\Program::constructors<Command, callable(Database): Command>() as { class: string, make: callable(Database): Command } $row) { echo ' ', $row->class, "\n";}Commands: Backup Migrate Report