Coming from C++ background ;)
How can I overload PHP functions?
One function definition if there are any arguments, and another if there are no arguments? Is it
In PHP 5.6 you can use the splat operator ...
as the last parameter and do away with func_get_args()
and func_num_args()
:
function example(...$args)
{
count($args); // Equivalent to func_num_args()
}
example(1, 2);
example(1, 2, 3, 4, 5, 6, 7);
You can use it to unpack arguments as well:
$args[] = 1;
$args[] = 2;
$args[] = 3;
example(...$args);
Is equivalent to:
example(1, 2, 3);