• PHP Video Tutorials

PHP - forward_static_call_array()



The forward_static_call_array() function can call a static method and pass the arguments as an array.

Syntax

mixed forward_static_call_array( callable $function , array $parameters )

The forward_static_call_array() function can call a user-defined function or method given by the function parameter. It must be called within a method context and can't be used outside the class. It can use the late static binding. All arguments of the forwarded method are passed as values and as an array similar to the call_user_func_array() function.

The forward_static_call_array() function can return the function result, or false on error.

Example

<?php
   class TestClass {
      private $obj = NULL;
      public function __construct() {
         $this->obj = new SubClass();    
         $this->obj->SetExtra(array('Karel', 'Anton'));
      }
      public function test() {
         forward_static_call_array([$this->obj, 'callMe'], func_get_args());
      }
   }

   class SubClass {
      private $SetVar = NULL;
      public function callMe() {
         $Array = $this->ArrayStrUp(array_merge(func_get_args(), $this->SetVar));
         echo 'YES WE FETCHED : '.PHP_EOL.print_r($Array, true);
      }
      public function SetExtra($vars){
         $this->SetVar = $vars;
      }

      private function ArrayStrUp($Arr) {
         foreach($Arr as $key => $value) {
            if(is_array($value) === true) {
               $Arr[$key] = $this->ArrayStrUp($value);
            } else {
               $Arr[$key] = strtoupper($value);
            }
         }
         return($Arr);
      }
   }
   $test = new TestClass();
   $test->test('John', 'Doe', array('Peter', 'Dora'), array('Anthony', 'William'));
?>

Output

YES WE FETCHED : 
Array
(
    [0] => JOHN
    [1] => DOE
    [2] => Array
        (
            [0] => PETER
            [1] => DORA
        )

    [3] => Array
        (
            [0] => ANTHONY
            [1] => WILLIAM
        )

    [4] => KAREL
    [5] => ANTON
)
php_function_reference.htm
Advertisements