Skip to content

Core\Reflect\MethodInfo::parameters

$methodInfo->parameters(): array<Core\Reflect\ParameterInfo>

Returns the parameters of a method, in the order they are written.

parameters() returns an array with one Core\Reflect\ParameterInfo for each parameter. $this is not included. Each ParameterInfo gives the name of the parameter with name(), without the $, and its declared type with type().

For a method that the compiler adds, such as the constructor of LogicError, the array is empty. Core\Reflect\MethodInfo::parameterCount still returns how many arguments that method takes.

The examples below print the parameters of a method, print their types, and check a web form for the fields that a handler needs.

Return value

Returns array<Core\Reflect\ParameterInfo>. One Core\Reflect\ParameterInfo for each parameter. $this is not included. For a method the compiler adds, such as the constructor of LogicError, the array is empty. parameterCount still returns how many arguments that method takes.

Examples

Print the parameter names

<?nvs
// Shows how to print the parameter names of every method of a class.
class Mailer
{
public function send(string $to, string $subject, int $retries): bool
{
return true;
}
public function ping(): bool
{
return true;
}
}
// This prints `ping:` with no names, and `send: to subject retries`.
foreach (Core\Reflect::forObject(new Mailer())->methods() as Core\Reflect\MethodInfo $method) {
echo $method->name(), ':';
foreach ($method->parameters() as Core\Reflect\ParameterInfo $parameter) {
echo ' ', $parameter->name();
}
echo "\n";
}
Output
ping:
send: to subject retries

Print the signature of a method

<?nvs
// Shows how to print a method with the name and the type of each parameter.
class Search
{
public function find(string $text, ?int $limit, array<string> $tags): array<string>
{
return $tags;
}
}
foreach (Core\Reflect::forObject(new Search())->methods() as Core\Reflect\MethodInfo $method) {
array<string> $parts = [];
foreach ($method->parameters() as Core\Reflect\ParameterInfo $parameter) {
// `type` returns the type as the method writes it, for example `?int`.
$parts[] = ($parameter->type() ?? '?') . ' $' . $parameter->name();
}
// This prints `find(string $text, ?int $limit, array<string> $tags)`.
echo $method->name(), '(', Core\Str::join($parts, ', '), ")\n";
}
Output
find(string $text, ?int $limit, array<string> $tags)

Check a form before calling its handler

<?nvs
// Shows how to check that a web form sent every field that its handler method needs.
class SignupHandler
{
public function handle(string $email, string $name, string $country): string
{
return 'Welcome, ' . $name;
}
}
class FormCheck
{
public static function missing(Core\Reflect\ClassInfo $info, array<string> $sent): array<string>
{
array<string> $missing = [];
foreach ($info->methods() as Core\Reflect\MethodInfo $method) {
if ($method->name() != 'handle') {
continue;
}
// Each parameter of `handle` is a field the form must send.
foreach ($method->parameters() as Core\Reflect\ParameterInfo $parameter) {
if (!Core\Arr::contains($sent, $parameter->name())) {
$missing[] = $parameter->name();
}
}
}
return $missing;
}
}
var $info = Core\Reflect::forObject(new SignupHandler());
// This form has no `country` field, so this prints `Missing: country`.
array<string> $sent = ['email', 'name'];
echo 'Missing: ', Core\Str::join(FormCheck::missing($info, $sent), ', '), "\n";
// This form has every field, so this prints `Missing: ` with nothing after it.
$sent[] = 'country';
echo 'Missing: ', Core\Str::join(FormCheck::missing($info, $sent), ', '), "\n";
Output
Missing: country
Missing: