Only arrays can be unpacked in constant expression

Description

... may be used on both arrays and Traversable objects, but within a static constant expression, this operator only works on arrays, and cannot be used on objects, Traversable or not.

Example

<?php

const A = 2;

function foo($a = [...A]) {
    print_r($a);
}

foo();

?>

Solutions

  • Rewrite the expression without the ... operator.

  • Convert the object into an array.