-
Bug
-
Resolution: Unresolved
-
High
-
None
PHP 8.0 added support for named arguments. These are a wonderous creation and allow things like the following:
class Greeter {
|
public function greetPerson(
|
string $name,
|
?int $age = null,
|
array $pets = [],
|
): void {
|
printf(
|
"Hello %s. I hear that you're %d years old. You have %d pets.",
|
$name,
|
$age,
|
count($pets),
|
);
|
}
|
}
|
|
$greeter = new Greeter();
|
$greeter->greetPerson(
|
name: "John",
|
// Argument order doesn't matter!!!
|
pets: ["dog", "cat", "fish"],
|
age: 42,
|
);
|
But when we are told that we shouldn't be collecting age information, and we must drop the argument, our current deprecation policy has us do this:
class Greeter {
|
public function greetPerson(
|
string $name,
|
?int $unused = null,
|
array $pets = [],
|
): void {
|
if ($unused !== null) {
|
debugging(
|
"The age argument is deprecated",
|
DEBUG_DEVELOPER,
|
);
|
}
|
printf(
|
"Hello %s. You have %d pets.",
|
$name,
|
count($pets),
|
);
|
}
|
}
|
|
$greeter = new Greeter();
|
$greeter->greetPerson(
|
name: "John",
|
// Argument order doesn't matter!!!
|
pets: ["dog", "cat", "fish"],
|
age: 42,
|
);
|
But oh no!!! This now makes the calling code explode because we are supplying an argument which does not exist:
Fatal error: Uncaught Error: Unknown named parameter $age in /in/WjMLa:28
|
Stack trace:
|
#0 {main}
|
thrown in /in/WjMLa on line 28
|
|
Process exited with code 255.
|
Therefore we must amend our deprecation policy to no longer allow the rename of unused arguments.