Named arguments

← Named arguments

Ashterix

docs: English JsonRpcBundle docs, REST and MCP Bundle sections, package cards

en/articles/from-tg/series/php-can-do/named-arguments.md new file +92 βˆ’0

1 + # Named arguments
2 +
3 + *Order no longer matters*
4 +
5 + - **Added in:** PHP 8.0
6 + - **What it gives you:** the ability to pass arguments to functions and methods by specifying their names, not just their values.
7 +
8 + ## What it's about
9 +
10 + Before PHP 8 you either had to remember the exact order of arguments or keep peeking at the method signature so as not to mix up which parameter goes after which. And if you needed to pass the last of several optional parameters, you had to pass all of them.
11 +
12 + With named arguments you can pass only the parameters you need β€” by name, and in any order too.
13 +
14 + ## Advantages
15 +
16 + - the code becomes more self-documenting β€” it's clear what each argument means;
17 + - no need to pass a pile of unneeded parameters just to change the last one;
18 + - less risk of mixing up the order;
19 + - easier refactoring β€” old calls don't break if the order of parameters changes.
20 +
21 + ## Real-world examples
22 +
23 + ```php
24 + function subscribe(
25 + string $email,
26 + ?string $name = null,
27 + ?string $language = 'uk',
28 + bool $doubleOptIn = true,
29 + ?string $source = null,
30 + ?string $tag = null,
31 + bool $sendWelcomeLetter = true,
32 + )
33 + { /* ... */ }
34 +
35 + // before 8.0 you have to pass everything, even what you don't need ❌
36 + subscribe('alex@example.com', null, 'uk', true, null, null, false);
37 +
38 + // since 8.0 you can stick to what you need βœ…
39 + subscribe('alex@example.com', sendWelcomeLetter: false);
40 +
41 + // or even pass the arguments in any order
42 + subscribe('alex@example.com', sendWelcomeLetter: false, tag: 'promo2025');
43 + ```
44 +
45 + The remaining parameters take their defaults, and the code reads like a specification of the method call.
46 +
47 + ### An example from a framework (Symfony)
48 +
49 + ```php
50 + protected function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
51 + { /* ... */ }
52 +
53 + //....
54 + return $this->redirectToRoute(
55 + route: 'user.profile',
56 + status: 301,
57 + parameters: ['id' => $user->getId()]
58 + );
59 + ```
60 +
61 + The order of the arguments doesn't matter β€” what matters is that it's clear what you're setting.
62 +
63 + :::warning
64 + It's worth noting that you shouldn't reorder arguments without a reason just because PHP allows it. If you see places like that in the code, better refactor them and restore the natural order of parameters.
65 +
66 + IDEs such as PhpStorm usually highlight such calls for a reason β€” it's a hint that it's time to tidy up.
67 + :::
68 +
69 + ### Calling an API or SDK
70 +
71 + ```php
72 + $client->sendMessage(
73 + to: '380501234567',
74 + text: 'Hi πŸ‘‹',
75 + channel: ChannelsEnum::TELEGRAM,
76 + );
77 + ```
78 +
79 + Every argument is visible at once, without comments or documentation.
80 +
81 + ## Conclusion
82 +
83 + > Named arguments are a configuration-style way of calling functions that makes code readable and safe to change.
84 + > No more threading a pile of nulls through just to reach the parameter you need.
85 +
86 + If a function has more than three parameters β€” you know what to do.
87 +
88 + ← [Previous: DateTimeImmutable](/articles/from-tg/series/php-can-do/datetime-immutable) Β· [Whole series](/articles/from-tg/series/php-can-do) Β· [Next: Enums β†’](/articles/from-tg/series/php-can-do/enums)
89 +
90 + ---
91 +
92 + *First published in Ukrainian in the Telegram channel [β€œCreated to die”](https://t.me/php_while_true/83) on October 23, 2025.*