php.net |  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
Votes:6
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
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: david at grudl dot com
New email:
PHP Version: OS:

 

 [2020-11-06 13:25 UTC] david at grudl dot com
Description:
------------
I quote directly from RFC: https://wiki.php.net/rfc/named_params

> 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.





Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-11-06 14:40 UTC] requinix@php.net
-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] nikic@php.net
-Status: Open +Status: Verified
 [2020-11-09 09:47 UTC] nikic@php.net
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: Thu Apr 18 03:01:28 2024 UTC