|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #80327 Conflicts of parameter names and forwarded variadics
Submitted: 2020-11-06 13:25 UTC Modified: 2020-11-09 09:47 UTC
Avg. Score:4.7 ± 0.5
Reproduced:4 of 5 (80.0%)
Same Version:4 (100.0%)
Same OS:2 (50.0%)
From: david at grudl dot com Assigned:
Status: Verified Package: Scripting Engine problem
PHP Version: 8.0.0RC3 OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
Solve the problem:
4 - 1 = ?
Subscribe to this entry?

 [2020-11-06 13:25 UTC] david at grudl dot com
I quote directly from RFC:

> One of the primary use-cases for that variadic/unpacking syntaxes is forwarding of arguments. The support for named arguments in both variadics and argument unpacking ensures that this pattern will continue to work once named arguments are introduced.

function passthru(callable $c, ...$args) {
    return $c(...$args);

Unfortunately, there is one limitation for forwarding at this time. You cannot pass argument with key 'c' to $args because it conflicts with parameter $c. So this

passthru($callable, $args);

May throws "Uncaught Error: Named parameter $c overwrites previous argument." depending on the content of (the otherwise valid) $args. 

How to solve it?

A possible solution is to use a very non-conflicting name for $c, for example passthru(callable $██, ...$args), but I think that a better solution would be to ignore the conflicts if positional parameters are passed and variadics follows.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-06 14:40 UTC]
-Summary: Conflicts of parameter names and variadics +Summary: Conflicts of parameter names and forwarded variadics -Type: Feature/Change Request +Type: Bug
 [2020-11-09 09:47 UTC]
-Status: Open +Status: Verified
 [2020-11-09 09:47 UTC]
Nice catch! I don't think we will address this for PHP 8.0, but generally agree that the rules around named parameters in conjunction with variadics should probably be relaxed.

There is also the related problem, independent of the forwarding use-case, that it's not really possible to use positional variadics in conjunction with named parameters. That is, doing something like this is not legal:

    array_push(array: $bar, 1, 2, 3)

Again, we could allow collecting the extra positional arguments into the variadic here.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Jun 21 16:01:30 2024 UTC