PHP 8.3.4 Released!

forward_static_call_array

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

forward_static_call_array调用静态方法且参数作为数组传递

说明

forward_static_call_array(callable $callback, array $args): mixed

通过 callback 参数指定调用用户定义的函数或者方法。此函数必须在方法上下文中调用,不能在类外使用。它使用后期静态绑定。转发方法的所有参数都作为值和数组传递,类似于 call_user_func_array()

参数

callback

要调用的函数或者方法。此参数可以是带类名及方法的 array 或者带函数名的 string

parameter

参数,将所有方法参数聚合到一个数组中。

注意:

注意 forward_static_call_array() 的参数不是通过引用传递的。

返回值

返回函数的结果,失败时返回 false

示例

示例 #1 forward_static_call_array() 示例

<?php

class A
{
const
NAME = 'A';
public static function
test() {
$args = func_get_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(array('A', 'test'), array('more', 'args'));
forward_static_call_array( 'test', array('other', 'args'));
}
}

B::test('foo');

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

?>

以上示例会输出:

B
B more,args 
C other,args

参见

add a note

User Contributed Notes 2 notes

up
2
nino dot skopac at gmail dot com
7 years ago
Regarding namespaces:

Be sure to include fully namespaced class path:

<?php
forward_static_call_array
(
array(
'NAMESPACE\CLASS_NAME', 'STATIC_METHOD'),
$params
);
up
0
israfilov93 at gmal dot com
5 years ago
one of academic example, when forward_static_call() can be useful

<?php

class A
{
public static function
test()
{
var_dump('we were here');
return static::class;
}
}

class
B extends A
{
public static function
test()
{
return
self::class;
}
}

class
C extends B
{
public static function
test()
{
$grandParent = get_parent_class(parent::class); // $grandParent is A
return forward_static_call([$grandParent, __FUNCTION__]); // calls A::test()
}
}

// prints
// string(12) "we were here"
// string(1) "C"
var_dump(C::test());
To Top