The forward_static_call_array Function
The forward_static_call_array
function allows you to call a static class method by passing parameters as an array. The first parameter accepts a callable (the class name and method in array or string format), the second - an array of arguments to pass to the method.
Syntax
forward_static_call_array(callable $function, array $parameters);
Example
Calling a static class method with parameter passing:
<?php
class Calculator {
public static function add($a, $b) {
return $a + $b;
}
}
$res = forward_static_call_array(['Calculator', 'add'], [5, 3]);
echo $res;
?>
Code execution result:
8
Example
Using a string call instead of an array:
<?php
class StringHelper {
public static function concat($str1, $str2) {
return $str1 . $str2;
}
}
$res = forward_static_call_array('StringHelper::concat', ['Hello', 'World']);
echo $res;
?>
Code execution result:
'HelloWorld'
See Also
-
the
call_user_func_array
function,
which calls a callback function with an array of parameters -
the
forward_static_call
function,
which calls a static method with individual arguments