The forward_static_call Function
The forward_static_call
function allows you to call a static class method,
using Late Static Binding. The first parameter
is a callback function in the form of an array [class, method] or a string with the method name,
and the subsequent parameters are the arguments for the method being called.
Syntax
forward_static_call(callable $callback, mixed ...$args): mixed
Example
Calling a static method via forward_static_call:
<?php
class A {
public static function test() {
return static::class;
}
}
class B extends A {}
$res = forward_static_call(['B', 'test']);
echo $res;
?>
Execution result:
'B'
Example
Passing arguments to the called method:
<?php
class Calculator {
public static function add($a, $b) {
return $a + $b;
}
}
$res = forward_static_call(['Calculator', 'add'], 5, 3);
echo $res;
?>
Execution result:
8
See Also
-
the
call_user_func
function,
which calls a callback function -
the
forward_static_call_array
function,
which is similar to forward_static_call but accepts arguments as an array