Skip to content

Core\Reflect\ParameterInfo::name

$parameterInfo->name(): string

Returns the name of one parameter of a method.

name() returns the name that the method declares, without the $. For send(string $to), the name is to. This is also the name you write for a named argument.

A constructor parameter that is also a property, such as constructor(private string $from), has the same name as the property.

You get a Core\Reflect\ParameterInfo from Core\Reflect\MethodInfo::parameters, which lists the parameters in the order they are written.

The examples below print the parameters of a method with a $, show a constructor parameter that is also a property, and call a method with values read from a configuration by name.

Return value

Returns string. The name as the method declares it, without the $. This is the name you write for a named argument. A promoted constructor parameter has the same name as its property.

Examples

Write the parameters with a dollar sign

<?nvs
// Shows that `name` returns the name of a parameter without the `$`.
class Mailer
{
public function send(string $to, string $subject): bool
{
return true;
}
}
foreach (Core\Reflect::forObject(new Mailer())->methods() as Core\Reflect\MethodInfo $method) {
foreach ($method->parameters() as Core\Reflect\ParameterInfo $parameter) {
// This prints `to` next to `$to`, and `subject` next to `$subject`.
echo $parameter->name(), ' is written as $', $parameter->name(), "\n";
}
}
Output
to is written as $to
subject is written as $subject

A constructor parameter that is a property

<?nvs
// Shows that a constructor parameter that is also a property has the name of the property.
class Mailer
{
public function constructor(private string $from, public int $retries)
{}
}
var $info = Core\Reflect::forObject(new Mailer('shop@example.com', 3));
// This prints the parameters of the constructor: `from` and `retries`.
foreach ($info->methods() as Core\Reflect\MethodInfo $method) {
foreach ($method->parameters() as Core\Reflect\ParameterInfo $parameter) {
echo 'parameter: ', $parameter->name(), "\n";
}
}
// This prints the properties. They have the same two names.
foreach ($info->properties() as Core\Reflect\PropertyInfo $property) {
echo 'property: ', $property->name(), "\n";
}
Output
parameter: from
parameter: retries
property: from
property: retries

Call a method with values from a config

<?nvs
// Shows how to call a method with values from a configuration, matched to its parameters by name.
class Pool
{
public function open(int $size, int $timeout): string
{
return 'pool of ' . $size . ', timeout ' . $timeout . 's';
}
}
// The keys are in a different order than the parameters.
array<int> $config = ['timeout' => 30, 'size' => 8];
var $pool = new Pool();
var $info = Core\Reflect::forObject($pool);
foreach ($info->methods() as Core\Reflect\MethodInfo $method) {
// Each argument is the value whose key is the parameter name.
array<int> $arguments = [];
foreach ($method->parameters() as Core\Reflect\ParameterInfo $parameter) {
$arguments[] = $config[$parameter->name()];
}
// This prints `pool of 8, timeout 30s`.
echo $info->call($pool, $method->name(), $arguments) as string, "\n";
}
Output
pool of 8, timeout 30s