Cannot use positional argument after named argument during unpacking¶
Description¶
This error appears when using an array with a mix of integer and string keys, and spread it as arguments. The actual order of the array is used as the order of the arguments in the function call. Then, integer keys are used as positional arguments, and string keys are as named arguments.
Example¶
<?php
function foo($a, $b, $c) {}
// unpacking argument, but positional argument is misplaced
$arguments = ['a' => 1, 2, 'c' => 3];
foo(...$arguments);
// make everyone positional. It works since order is already correct
foo(...array_values($arguments));
?>
Solutions¶
Add the missing argument names to finish the argument array.
Move the positional argument to the beginning of the array (array_unshift, or append it at the array creation), when the argument order makes it possible.
Use ksort() on the keys, when it makes sense.