|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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. PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Oct 25 11:00:01 2025 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.