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.

php Courses
php Courses
php Courses
Using Named Parameters in PHP 8

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.

request(
    string $method,
    string $uri,
    array $parameters = [],
    array $files = [],
    array $server = [],
    string $content = null,
    bool $changeHistory = true
): Crawler

In PHP 7, sending a POST request with many parameters required a long argument list, as illustrated:

$client->request(
    'POST',
    '/foo',
    [],
    [],
    ['CONTENT_TYPE' => 'application/json'],
    '{"data":[]}'
);

With PHP 8 named parameters, the same request becomes more concise:

$client->request(
    method: 'POST',
    uri: '/foo',
    server: ['CONTENT_TYPE' => 'application/json'],
    content: '{"data":[]}'
);

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.

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"
}
*/

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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendmigrationPHPCode ExamplesPHP8named_parameters
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.