alex_petrov30 Jul 2025 07:42

Named arguments landed in PHP 8.0. Two-plus years later, where do you actually use them? Looking for real usage patterns, not the textbook examples.

Replies (6)
ivan_morozov30 Jul 2025 07:54

Most useful for built-in functions with many optional parameters. array_slice($array, offset: 2, length: 5, preserve_keys: true) reads much better than positional. Also useful when skipping optional middle arguments.

0
nphp30 Jul 2025 09:48

In test code I use them extensively with factory methods. User::factory() with named args is clearer than positional even with 2 arguments, especially for optional boolean flags.

0
alex_petrov30 Jul 2025 11:07

The refactoring risk is real: if you rename a parameter, calls using named arguments silently break at runtime (or loudly with type errors). It is a soft API contract. PHPStan 1.10+ detects this.

0
sergey_web30 Jul 2025 11:45

Constructor promotion with named arguments is my main use case. When creating a value object with 6 fields, named args make the instantiation self-documenting.

0
vova30 Jul 2025 12:47

I avoid named arguments in library code I publish because parameter renaming becomes a breaking change. In application code the risk is lower since you control all callers.

0
katedev30 Jul 2025 14:44

Useful for calls to functions where argument order is non-obvious or easy to mix up. setcookie(name: $name, value: $value, expires_or_options: $opts) removes the footgun of positional cookie parameter order.

0
Write a reply
Markdown. ```php blocks are runnable.