1.2.931. PHP 80 Named Parameter Variadic

Named parameter with variadic have been renamed from 0 to ‘parameter name’ in PHP 8.0.

In PHP 7.0, with positional argument only, the content of $b is in an array, index 0. This is also true with PHP 8.0.

In PHP 8.0, with named arguments, the content of $b is in an array, index ‘b’;

Since the behavior of the variadic depends on the calling syntax (with or without named parameter), the receiving must ensure the correct reception, and handle both cases.

<?php

function foo($a, ...$b) {
    print_r($b);
}

foo(3, 4);
foo(3, b: 4);              // PHP 8 only
foo(...[2, "b"=> [3, 4]]); // PHP 8 only

?>

1.2.931.1. Suggestions

  • Apply array_values() to the variadic arguments.

1.2.931.2. Specs

Short name

Php/Php80NamedParameterVariadic

Rulesets

All, CE, CompatibilityPHP80

Exakat since

2.2.0

PHP Version

All

Severity

Minor

Time To Fix

Quick (30 mins)

Precision

Medium

Features

variadic, parameter

Available in

Entreprise Edition, Community Edition, Exakat Cloud