Arrow Functions. Arrow functions were introduced in PHP 7.4 as a more concise syntax for anonymous functions. Both anonymous functions and arrow functions are implemented using the Closure class. Arrow functions have the basic form fn (argument_list) => expr. Arrow functions support the same features as anonymous functions, except that using variables from the parent scope is always automatic.
Func Get Arg - PHP: Function arguments - Manual
If you have your arguments in an array, you might be interested by the function.call_user_func_array
If the number of arguments you want to pass depends on the length of an array, it probably means you can pack them into an array themselves -- and use that one for the second parameter of .call_user_func_array
Elements of that array you pass will then be received by your function as distinct parameters.
For instance, if you have this function :function test() {
var_dump(func_num_args());
var_dump(func_get_args());
}
You can pack your parameters into an array, like this :
$params = array(
10,
'glop',
'test',
);
And, then, call the function :
call_user_func_array('test', $params);
This code will the output :
int 3
array
0 => int 10
1 => string 'glop' (length=4)
2 => string 'test' (length=4)
ie, 3 parameters ; exactly like iof the function was called this way :
test(10, 'glop', 'test');
This is literally called the operator in PHP, but is known as the splat operator from other languages. From a 2014 LornaJane blog post on the feature:...
This feature allows you to capture a variable number of arguments to a function, combined with "normal" arguments passed in if you like. It's easiest to see with an example:
function concatenate($transform, ...$strings) { $string = ''; foreach($strings as $piece) { $string .= $piece; } return($transform($string)); } echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");
(This would print )I'D LIKE 6 APPLES
The parameters list in the function declaration has the
operator in it, and it basically means " ... and everything else should go into...". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings$strings, ready to be used.array
You can use Reflection :
function get_func_argNames($funcName) {
$f = new ReflectionFunction($funcName);
$result = array();
foreach ($f->getParameters() as $param) {
$result[] = $param->name;
}
return $result;
}
print_r(get_func_argNames('get_func_argNames'));
//output
Array
(
[0] => funcName
)
Much like the manual, use an equals () sign in your definition of the parameters:=
function dosomething($var1, $var2, $var3 = 'somevalue'){
// Rest of function here...
}