Named Arguments After Argument Unpacking in PHP 8.1

Named Arguments After Argument Unpacking in PHP 8.1

Since PHP 8.0, we can use named arguments. These arguments can be passed to a function or class method based on the parameter name. In PHP 8.0, combining named arguments with argument unpacking is not allowed. It produces an error.

Since PHP 8.1, we can use named arguments after argument unpacking.

<?php

function printNumbers(int $a, int $b, int $c) {
    echo $a.' '.$b.' '.$c;
}

printNumbers(...[5, 7], c: 9); // Output: 5 7 9

However, argument unpacking after named arguments still cannot be used.

<?php

function printNumbers(int $a, int $b, int $c) {
    echo $a.' '.$b.' '.$c;
}

printNumbers(a: 5, ...[7, 9]); // Error

Leave a Comment

Cancel reply

Your email address will not be published.