I have this code:
function test(...$strings)
{
// ...
}
It allows me to call test() like this:
test('a', 'series of', 'strings go here');
Which works. However, I often would like to do:
test([ 'a', 'series of', 'strings go here' ]);
Or:
test($an_array_of_strings);
In fact, I was so sure that this worked that I was shocked and confused when I started getting errors about "Array to string conversion".
I mean, the "..." syntax is a special language construct specifically to turn a variable number of arguments to a function into an array! I don't even understand why it would not automatically understand this very common/useful practice.
Since it (apparently) doesn't, is there some way to accomplish this without having to use two separate functions? I don't want a separate:
function test_array($strings = [])
{
// ...
}
...
operator to splat the input too -test(...$an_array_of_strings)
ortest(...[ 'a', 'series of', 'strings go here' ])
. – Case