http://php.net/manual/en/function.array-push.php clearly shows the signature of the function with the variadic being completely optional:
int array_push ( array &$array [, mixed $... ] )
I think that, when using array_push
with splat operator, it would be extremely desired for it to accept empty variadic, e.g. to allow such code:
<?php
function add( array $arr1, array $arr2 ) {
return \array_push($arr1, ...$arr2);
}
echo add(['foo'], ['bar']); // OK, returns 2
echo add(['foo'], []); // should be OK, but ain't currently - raises an E_WARNING
?>
Many languages (e.g. Java) allow empty variadics in functions (usually resulting in a no-op) for that exact reason. PHP doesn't, at least not in PHP 7.1 nor 7.2.
OTOH, although sharing a similar syntax definition, array_merge
and many other accept the empty variadic and work correctly using the aformentioned splat syntax:
var_dump( array_merge(['foo'], ...[]) ); // works for me.
My questions are:
- is this actually a intended behaviour and a documentation error, a bug in implementation, or something else?
- (a) if this is an intended behaviour, what's the rationale behind it in light of the above?
- (b) if this is a bug, why does it happen?
note: I've checked the docs for other array functions, and e.g. http://php.net/manual/en/function.compact.php, http://php.net/manual/en/function.array-merge.php etc. show the param list exactly as expected).