PHP Function Reference

PHP forward_static_call_array() Function



The PHP forward_static_call_array() 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.

All arguments of the forwarded method are passed as values, and as an array, similarly to call_user_func_array().

Syntax

forward_static_call_array(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 one parameter, gathering all the method or function parameters in an array.

Return Value

Returns the function result, or false on error.

Example: forward_static_call_array() example

The example below shows the usage of forward_static_call_array() 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_array() function
class C extends A {
  const NAME = 'C';

  public static function test() {
    echo self::NAME."\n";
    forward_static_call_array(array('A', 'test'), array('more', 'args'));
    forward_static_call_array('test', array('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_array() example

Consider one more example explaining the usage of forward_static_call_array() 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_array() function
class C extends A {
  const NAME = 'C';

  public static function test() {
    echo self::NAME."\n";
    forward_static_call_array(array('A', 'test1'), array('more', 'args'));
    forward_static_call_array('test2', array('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