PHP Function Reference

PHP forward_static_call() Function



The PHP forward_static_call() function is used to call a user defined static function or method, with the specified arguments. This function must be called within a method context, it can't be used outside a class. It uses the late static binding.

Syntax

forward_static_call(callback, args)

Parameters

callback Required. Specify the function or method to be called. It can be an array, with the name of the class, and the method, or a string, with a function name.
args Required. Specify zero or more parameters to be passed to the function.

Return Value

Returns the function result, or false on error.

Example: forward_static_call() example

The example below shows the usage of forward_static_call() function.

<?php
//defining a class with static method
class A {
  const NAME = 'A';
  public static function test() {
    $args = func_get_args();
    echo static::NAME." ".join(',', $args)." \n";
  }
}

//defining a static function
function test() {
  $args = func_get_args();
  echo "B ".join(',', $args)." \n";
}

//defining a class which uses above defined 
//static method and function using 
//forward_static_call() function
class C extends A {
  const NAME = 'C';

  public static function test() {
    echo self::NAME."\n";
    forward_static_call(array('A', 'test'), 'more', 'args');
    forward_static_call('test', 'other', 'args');
  }
}

//calling (test) method of class C which
//calls static method (test) of class A
//and static function (test) inside
C::test('foo');
?>

The output of the above code will be:

C
C more,args 
B other,args 

Example: forward_static_call() example

Consider one more example explaining the usage of forward_static_call() function.

<?php
//defining a class with static method
class A {
  const NAME = 'A';
  public static function test1($arg1, $arg2) {
    echo static::NAME." got $arg1 $arg2 \n";
  }
}

//defining a static function
function test2($arg1, $arg2) {
  echo "B got $arg1 $arg2 \n";
}

//defining a class which uses above defined 
//static method and function using 
//forward_static_call() function
class C extends A {
  const NAME = 'C';

  public static function test() {
    echo self::NAME."\n";
    forward_static_call(array('A', 'test1'), 'more', 'args');
    forward_static_call('test2', 'other', 'args');
  }
}

//calling (test) method of class C which
//calls static method (test1) of class A
//and static function (test2) inside
C::test();
?>

The output of the above code will be:

C
C got more args 
B got other args 

❮ PHP Function Handling Reference