文字

forward_static_call

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

forward_static_callCall a static method

说明

mixed forward_static_call ( callable $function [, mixed $parameter [, mixed $... ]] )

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

参数

function

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.

parameter

Zero or more parameters to be passed to the function.

返回值

Returns the function result, or FALSE on error.

范例

Example #1 forward_static_call() example

<?php

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

class 
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_get_args ();
        echo 
"C " . join ( ',' $args ). " \n" ;
    }

?>

以上例程会输出:

B
B more,args 
C other,args

参见

  • forward_static_call_array() - Call a static method and pass the arguments as array
  • call_user_func_array() - 调用回调函数,并把一个数组参数作为回调函数的参数
  • call_user_func() - 把第一个参数作为回调函数调用
  • is_callable() - 检测参数是否为合法的可调用结构
  • callback 类型的信息

用户评论:

[#1] jhibbard at gmail dot com [2012-06-19 19:55:25]

Example usage via calls outside of the class and within an object:

<?php

class foo {
  
# used to verify we're actually setting something..
  
private static $value '';

  

  
public static function set($method_identifier$value_to_pass '') {
    
# make sure we have the right method format...
    # another semi-useful example is like this (useful for REST-like 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 work 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);
    }
  }

  

  
public static function setValue($value_recieved '') {
    echo 
"\t\tsetValue called with param of " var_export($value_recievedtrue) . "!\n";

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

    
self::$value $value_recieved;

    echo 
"\t\tChecking 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 '';
  }

  

  
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();
?>

[#2] arthur dot techarts at gmail dot com [2011-07-12 07:39:38]

Example to understand this function and difference with call_user_func:

<?php
class Beer {
    const 
NAME 'Beer!';
    public static function 
printed(){
        echo 
'static Beer:NAME = '. static::NAME PHP_EOL;
    }
}

class 
Ale extends Beer {
    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>';
?>

上一篇: 下一篇: