php / php/doc-en

Using named parameters with a variadic parameter

Open
#2,027 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
XML
Stars
596
Forks
890
Avg merge
1d 15h
Merged PRs (30d)
55

Description

Description

Consider the following code:

function foo(int $a = 0, int $b = 0, string ...$flags) {}

It is not possible to pass more than one value to $flags when using named parameters:

foo(flags: 'bar'); // valid

foo(flags: 'bar', 'baz'); // Fatal error: Cannot use positional argument after named argument

foo(flags: ['bar', 'baz']); // Fatal error: Uncaught TypeError: foo(): Argument php/php-src#3 must be of type string, array given

foo(flags: ...['bar', 'baz']); // Parse error: syntax error, unexpected token "..."

Additionally, foo(flags: 'bar'); results in the $flags being:

[
    'flags': 'bar',
]

Not sure if this is expected but I expected it to be ['bar'].

PHP Version

8.0+

Operating System

No response

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no documentation file or test; start by locating the PHP 8 documentation for named arguments and variadic parameters. Compare its documented behavior with the supplied foo() examples, and consider the work done when the supported syntax and resulting variadic array keys are explained accurately.

Written by the indexing model from the issue text.

Assessment

Tech stack
php
Domain
documentation
Issue type
Documentation
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.