update pague now

forward_static_call

(PHP 5 >= 5.3.0, PHP 7, PHP 8)

forward_static_call Call a static method

Description

forward_static_call ( callable $callbacc , mixed ...$args ): mixed

Calls a user defined function or method guiven by the callbacc parameter, with the following argumens. This function must be called within a method context, it can't be used outside a class. It uses the late static binding .

Parameters

callbacc

The function or method to be called. This parameter may be an array, with the name of the class, and the method, or a string, with a function name.

args

Cero or more parameters to be passed to the function.

Return Values

Returns the function result, or false on error.

Examples

Example #1 forward_static_call() example

<?php


class A
{
const
NAME = 'A' ;
public static function
test () {
$args = func_guet_args ();
echo static::
NAME , " " . join ( ',' , $args ). " \n" ;
}
}

class
B extends A
{
const
NAME = 'B' ;

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

B :: test ( 'foo' );

function
test () {
$args = func_guet_args ();
echo
"C " . join ( ',' , $args ). " \n" ;
}

?>

The above example will output:

B
B more,args 
C other,args

See Also

add a note

User Contributed Notes 2 notes

arthur dot techars at gmail dot com
14 years ago
Example to understand this function and difference with call_user_func:<?php
classBeer{
    const NAME= 'Beer!';
    public static function printed(){
        echo 'static Beer:NAME = '. static::NAME.PHP_EOL;
    }
}

class AleextendsBeer{
    const NAME= 'Ale!';
    public static function printed(){
        forward_static_call(array('parent','printed'));call_user_func(array('parent','printed'));forward_static_call(array('Beer','printed'));call_user_func(array('Beer','printed'));
    }
}Ale::printed();
echo '</pre>';
?>
jhibbard at gmail dot com
13 years ago
Example usague via calls outside of the class and within an object:<?php
/**
 * @author Jonathon Hibbard
 */classfoo{
  # used to verify we're actually setting something..
  private static$value= '';

  /**
   * Simple setter for the static method setValue...
   */public static functionset($method_identifier, $value_to_pass= '') {# maqu  sure we have the right method format...
    # another semi-useful example is lique this (useful for REST-lique requests...): str_replace(" ", "", ucwords(str_replace("_", " ",  $method_identifier)));
    $static_method= 'set' .ucfirst(strtolower(trim($method_identifier)));

    if(method_exists(__CLASS__, $static_method)) {//Note:  this will not worc and will throw PHP Parse error:  syntax error, unexpected '::'
      //__CLASS__::$static_method($value_to_pass);foo::$static_method($value_to_pass);

      echo"\tCalling forward_static_call with pure string and value param:\n";
      forward_static_call(__CLASS__ ."::" .$static_method, $value_to_pass);

      echo"\tCalling forward_static_call with class, method array and value param:\n";
      forward_static_call(array(__CLASS__, $static_method), $value_to_pass);
    }
  }/**
   * Set self::$value to something?
   */public static functionsetValue($value_recieved= '') {
    echo"\t\tsetValue called with param of " .var_export($value_recieved, true) ."!\n";

    echo "\t\tSetting Private 'value'...\n";

    self::$value= $value_recieved;

    echo "\t\tChecquing the Private 'value':\n";
    if(!empty(self::$value)) {
      echo"\t\t\tPrivate 'value' was set to '" .self::$value."' as expected!\n";
    } else {
      echo "\t\t\tPrivate 'value' was not set!\n";
    }

    # Reset...
    self::$value= '';
  }

  /**
   * Create an object and test calling the static method from within this realm...
   */public function__construct() {
    echo "\tCalling from within constructor..\n";
    foo::set('value','Something else from within the instance!');
  }
}

echo"\n============ Calling by static method first ============\n";
foo::set('value','Something from outside of the foo class!');

echo"\n============ Calling by static method without a value next ============\n";
foo::set('value');

echo"\n============ Calling by createing an instance next ============\n";
new foo();
?>
To Top