1.2.937. 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.937.1. Connex PHP features

1.2.937.1.1. Suggestions

  • Apply array_values() to the variadic arguments.

1.2.937.1.2. Specs

Short name

Php/Php80NamedParameterVariadic

Rulesets

All, CE, Changed Behavior, CompatibilityPHP80

Exakat since

2.2.0

PHP Version

With PHP 8.0 and more recent

Severity

Minor

Time To Fix

Quick (30 mins)

Changed Behavior

PHP 8.0+

Precision

Medium

Available in

Entreprise Edition, Community Edition, Exakat Cloud