Skip to content

Core\Reflect\ClassInfo::call

$classInfo->call(
    mixed $object,
    string $name,
    array<mixed> $arguments,
): mixed

Calls a method of an object when the method's name is in a string.

call($object, $name, $arguments) runs the method $name on $object and returns its result. $arguments is a list with one value for each parameter, in order. Use [] for a method with no parameters. The result has the type mixed, so convert it with as.

call follows the same rules as a normal method call at the same place in your code. A private method cannot be called from outside its class, and a wrong name or the wrong number of arguments throws an error. An argument of the wrong type throws a LogicError. call does not run a static method or a method with an inout parameter. Both throw a LogicError. You cannot switch the visibility check off.

The examples below show a call with two arguments, the errors for a private method and a wrong name, and a command line tool that runs the method named by a command.

Parameters

  • $objectmixedAn instance of the described class. The description is of a class and does not contain an object, so you pass the object to call here.
  • $namestringThe method's name without the (), as the declaration writes it. The text cannot be tainted. Check text from a request with Core\Taint::assertTrusted first.
  • $argumentsarray<mixed>One entry for each declared parameter, in order. Keys are ignored. Entries after the last parameter are ignored. This argument is required even when the method has no parameters. In that case, pass []. A tainted or secret value, or a mixed value, does not compile here.

Return value

Returns mixed. Whatever the method returned. Its type is mixed, whatever type the method declares.

Errors

  • LogicError$name is not public, is not a method of the class, or is a Core method. Or the method is static, or it has an inout parameter. Or $arguments has fewer entries than the method's parameters, or an entry has a type that its parameter does not accept. An ordinary call on a mixed value throws the same error, from the same check.
  • RuntimeError$object is not an object at all. A $object that is an object but not an instance of the described class is the LogicError above.

Changelog

VersionChange
0.0.6The 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

Call a method by name

<?nvs
// Shows how to call a method when its name is in a string.
class Greeter
{
public function greet(string $greeting, string $name): string
{
return $greeting . ', ' . $name . '!';
}
}
var $greeter = new Greeter();
var $info = Core\Reflect::forObject($greeter);
// `call` runs the method "greet" on `$greeter`. The list gives the arguments in order.
// This prints "Good morning, Ana!".
echo $info->call($greeter, 'greet', ['Good morning', 'Ana']) as string, "\n";
Output
Good morning, Ana!

A private method cannot be called

<?nvs
// Shows that `call` follows the same visibility rules as a normal method call.
class Account
{
public function label(): string
{
return 'Account ' . $this->number();
}
private function number(): string
{
return 'DE-4471';
}
}
var $account = new Account();
var $info = Core\Reflect::forObject($account);
// A public method works. This prints "Account DE-4471".
echo $info->call($account, 'label', []) as string, "\n";
// `number` is private, so code outside the class cannot call it.
// `call` throws a `LogicError`, the same as a normal call would.
try {
$info->call($account, 'number', []);
} catch (LogicError $error) {
echo "number is private\n";
}
// A name that is not a method also throws a `LogicError`.
try {
$info->call($account, 'lable', []);
} catch (LogicError $error) {
echo "no method lable\n";
}
Output
Account DE-4471
number is private
no method lable

Run a command from the command line

<?nvs
// Shows how a command line tool runs the method that has the same name as the
// command the user typed.
class Tasks
{
public function build(): string
{
return 'building the project';
}
public function clean(): string
{
return 'deleting old files';
}
}
class Runner
{
public static function run(object $tasks, string $command): tainted string
{
var $info = Core\Reflect::forObject($tasks);
// `hasMethod` checks the name first, so an unknown command gets a helpful message.
if (!$info->hasMethod($command)) {
return 'unknown command: ' . $command;
}
return $info->call($tasks, $command, []) as string;
}
}
// These are the commands a user typed. This prints one line for each.
array<string> $typed = ['build', 'clean', 'deploy'];
foreach ($typed as string $command) {
echo Runner::run(new Tasks(), $command), "\n";
}
Output
building the project
deleting old files
unknown command: deploy