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

phpforward_static_call(callable $callback, mixed ...$args): mixed

Example

Calling a static method via forward_static_call:

php<?php
class A {
  public static function test() {
    return static::class;
  }
}

class B extends A {}

$res = forward_static_call(['B', 'test']);
echo $res;
?>

Execution result:

php'B'

Example

Passing arguments to the called method:

php<?php
class Calculator {
  public static function add($a, $b) {
    return $a + $b;
  }
}

$res = forward_static_call(['Calculator', 'add'], 5, 3);
echo $res;
?>

Execution result:

php8

See Also