PHP Reference ยท Language Constructs and Statements
PHP named arguments
Reference for PHP named arguments (PHP 8.0): pass arguments by parameter name, skip defaults, mix with positional arguments, spread arrays and avoid errors.
Named arguments let you pass a value by the parameter's name instead of its position: makeUser(name: "Ada", admin: true). They arrived in PHP 8.0. The order no longer matters, and you can skip optional parameters in the middle without repeating their defaults.
They make calls self-documenting, especially for functions with several booleans or optional parameters, and they work for functions, methods, constructors and built-ins.
echo str_pad(string: "7", length: 3, pad_string: "0", pad_type: STR_PAD_LEFT); $u = new User(name: "Ada", admin: true);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with fgets(STDIN)), it gets the next line from this box.
Syntax
functionName(paramName: value, otherParam: value); functionName(positional, paramName: value); functionName(...['paramName' => value]);
| Rule | Description |
|---|---|
| Name | The parameter name without $. It is case-sensitive and must match the declaration exactly. |
| Mixing | Positional arguments first, named arguments after. Once you use a name, all following arguments must be named too. |
| Each parameter once | Passing the same parameter twice (by position and by name, or by name twice) throws an Error. |
| Skipping | Skipped parameters need a default value. Skipping a required parameter throws ArgumentCountError. |
| Spread | ...$array with string keys passes those entries as named arguments. |
Return value: whatever the function returns.
Errors: an unknown name throws Error ("Unknown named parameter $x"). Overwriting a positional argument throws Error. A missing required parameter throws ArgumentCountError.
Usage notes
- Parameter names become API. Renaming a parameter now breaks callers who use names. Library authors should treat names as part of the public interface;- Inheritance. Names are looked up on the method actually called. If a child class renames a parameter, calls using the parent's name fail on child objects, so keep names stable in overrides.
- Variadics. Extra named arguments are collected by a variadic parameter with string keys, as in
collect(). A function without a variadic rejects unknown names. - Attributes and constructors. Attributes and
newexpressions take named arguments too, which keeps configuration readable:#[Route(path: "/x", methods: ["GET"])]. - Booleans.
render($page, true, false)is obscure;render($page, cache: true, minify: false)is not. func_get_args()ignores names. It returns only positional values, so functions that rely on it do not mix well with named arguments.- With
call_user_func_array(), string keys in the array become named arguments. - Compatible with defaults. Skipped optional parameters get their defaults, including those that depend on constants or
newin initializers.