Using Named Parameters in PHP 8
PHP 8 introduces named parameters, allowing developers to pass arguments by name instead of position, which improves code readability, reduces errors, simplifies version migration, and enables more self‑documenting function calls, as demonstrated with several HTTP request examples and array‑handling snippets.
Named parameters are a new feature introduced in PHP 8 that lets you pass arguments to functions by their parameter names rather than by position, making code clearer, more readable, and less error‑prone.
To use named parameters, you declare the parameter names in the function signature and then call the function using the parameterName: value syntax. The article shows an example with a request() method.
<code>request(
string $method,
string $uri,
array $parameters = [],
array $files = [],
array $server = [],
string $content = null,
bool $changeHistory = true
): Crawler
</code>In PHP 7, sending a POST request with many parameters required a long argument list, as illustrated:
<code>$client->request(
'POST',
'/foo',
[],
[],
['CONTENT_TYPE' => 'application/json'],
'{"data":[]}'
);
</code>With PHP 8 named parameters, the same request becomes more concise:
<code>$client->request(
method: 'POST',
uri: '/foo',
server: ['CONTENT_TYPE' => 'application/json'],
content: '{"data":[]}'
);
</code>The article also discusses how named parameters improve consistency when using built‑in functions like array_map() and array_filter() , which have differing signatures that can be confusing.
An extended example shows a VideoGame class with a constructor using named parameters, followed by filtering and mapping a list of games based on the current year, demonstrating how named parameters simplify complex calls.
<code>class VideoGame
{
public function __construct(
public string $title,
public int $releaseYear,
) {}
}
$videoGames = [
new VideoGame(title: 'Marvel\'s Spider-Man 2', releaseYear: 2023),
new VideoGame(
title: 'Hogwarts Legacy : L\'Héritage de Poudlard',
releaseYear: 2023
),
new VideoGame(title: 'Elden Ring', releaseYear: 2022),
new VideoGame(title: 'Stray', releaseYear: 2022),
];
$currentYear = (int) date('Y');
$currentYearVideoGames = array_filter(
array: $videoGames,
callback: fn (VideoGame $game) => $game->releaseYear === $currentYear
);
$currentYearVideoGameTitles = array_map(
array: $currentYearVideoGames,
callback: fn (VideoGame $game) => $game->title
);
var_dump($currentYearVideoGameTitles);
/*
array(2) {
[0]=> string(21) "Marvel's Spider-Man 2"
[1]=> string(41) "Hogwarts Legacy : L'Héritage de Poudlard"
}
*/
</code>Named parameters also simplify version migrations; for example, the signature of implode() changed between PHP 7.4 and 8.0, but using named parameters avoids issues caused by reordered arguments.
Overall, named parameters reduce the need to remember argument order, make code self‑documenting, improve readability, and lower the risk of bugs.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.