PHP 7.4: Splat Inconsistency

PHP 7.4 has this great new feature: splat operator now works in array expressions.

<?php
var_export([1,2,3, ...[4,5,6]]); // [1,2,3,4,5,6]

including when it is in the middle

<?php
var_export([1,2,3, ...[4,5,6], 7,8,9]); // [1,2,3,4,5,6,7,8,9]

Let's have some variadic function

<?php

function sum(...$values)
{
    return array_sum($values);
}

but

<?php
// PHP 5.6 - 7.4
echo sum(1,2,3, ...[4,5,6], 7,8,9); // PHP Fatal error: Cannot use positional argument after argument unpacking

of course there is a well-known workaround

<?php
echo sum(1,2,3, ...[4,5,6], ...[7,8,9]); // 45

and now also this

<?php
// PHP 7.4+
echo sum(...[1,2,3, ...[4,5,6], 7,8,9]); // 45

but why wasn't the original behavior fixed as well?

Comments

Comments powered by Disqus