Core\Reflect\ClassInfo::construct
$classInfo->construct(array<mixed> $arguments): mixed$classInfo->construct( array<mixed> $arguments, ): mixed
Creates an object when the name of its class is in a string.
construct($arguments) creates a new object of the described class and runs its constructor. $arguments is a list with one value for each constructor parameter, in order. Use [] for a class with no constructor parameters. The result has the type mixed, so convert it with as.
construct follows the same rules as new at the same place in your code. A private constructor cannot be used from outside its class. Too few arguments, or an argument of the wrong type, throws a LogicError and no object is created. You cannot switch the visibility check off.
You cannot create an abstract class, an interface or a class of Core this way. For each of them, construct throws a LogicError.
The examples below show an object created from a class name, the errors for a private constructor and a missing argument, and a program that creates the handler its configuration names.
Parameters
$argumentsarray<mixed>One entry for each declared constructor parameter, in order. Keys are ignored. Entries after the last parameter are ignored. This argument is required even when the class declares no constructor. In that case, pass[]. Ataintedorsecretvalue, or amixedvalue, does not compile here.
Return value
Returns mixed. The new instance. Its type is mixed. For a class with no constructor, the new instance has the default values that the class declares for its properties.
Errors
LogicErrorThe constructor is notpublicand this call site is outside the class. Or$argumentshas fewer entries than the constructor's parameters, or an entry has a type that its parameter does not accept. Or this program does not declare the described class. Or the described class isabstract, an interface or a class ofCore, which this method cannot create. An ordinarynewmakes the same checks.
Changelog
| Version | Change |
|---|---|
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
Create an object from a class name
<?nvs// Shows how to create an object when the name of its class is in a string.
class Point{ public int $x; public int $y;
public function constructor(int $x, int $y) { $this->x = $x; $this->y = $y; }}
class Factory{ public static function make(string $className, array<int|float|string|bool> $arguments): mixed { var $info = Core\Reflect::forClass($className); if ($info == null) { return null; } // `construct` runs the constructor with the values in `$arguments`, in order. return $info->construct($arguments); }}
// The result has the type `mixed`, so `as Point` converts it. This prints "3, 4".var $point = Factory::make('Point', [3, 4]) as Point;echo $point->x, ', ', $point->y, "\n";3, 4A private constructor cannot be used
<?nvs// Shows that `construct` follows the same rules as `new`.
class Settings{ public string $file;
private function constructor(string $file) { $this->file = $file; }
// Code inside the class may use the private constructor. public static function load(): mixed { var $info = Core\Reflect::forClass('Settings'); if ($info == null) { return null; } return $info->construct(['app.toml']); }}
class Label{ public string $text;
public function constructor(string $text) { $this->text = $text; }}
// This works, because `load` is inside the class. This prints "app.toml".var $settings = Settings::load() as Settings;echo $settings->file, "\n";
// Code outside the class cannot use the private constructor.// `construct` throws a `LogicError`, the same as `new` would.var $settingsInfo = Core\Reflect::forClass('Settings');if ($settingsInfo != null) { try { $settingsInfo->construct(['other.toml']); } catch (LogicError $error) { echo "the constructor is private\n"; }}
// The constructor of `Label` needs one argument. An empty list throws a `LogicError`.var $labelInfo = Core\Reflect::forClass('Label');if ($labelInfo != null) { try { $labelInfo->construct([]); } catch (LogicError $error) { echo "an argument is missing\n"; }}app.tomlthe constructor is privatean argument is missingCreate the handler named in configuration
<?nvs// Shows how a program creates the handler class that its configuration names.
class ConsoleNotifier{ public string $prefix;
public function constructor(string $prefix) { $this->prefix = $prefix; }
public function send(string $text): string { return $this->prefix . ' ' . $text; }}
class EmailNotifier{ public string $address;
public function constructor(string $address) { $this->address = $address; }
public function send(string $text): string { return 'mail to ' . $this->address . ': ' . $text; }}
class Notifiers{ public static function send(string $className, string $option, string $text): tainted string { var $info = Core\Reflect::forClass($className); // `forClass` returns `null` for a name that is not a class. if ($info == null) { return 'unknown handler: ' . $className; } // The handler gets its option from the configuration as its constructor argument. mixed $handler = $info->construct([$option]); return $info->call($handler, 'send', [$text]) as string; }}
// Each row is one line of configuration: the class name and its option.// This prints one line for each row.array<array<string>> $configured = [ ['ConsoleNotifier', '[info]'], ['EmailNotifier', 'ops@example.com'], ['SmsNotifier', '+100'],];foreach ($configured as array<string> $row) { echo Notifiers::send($row[0], $row[1], 'backup finished'), "\n";}[info] backup finishedmail to ops@example.com: backup finishedunknown handler: SmsNotifier