Regarding namespaces:Be sure to include fully namespaced class path:<?phpforward_static_call_array( array('NAMESPACE\CLASS_NAME', 'STATIC_METHOD'), $params);
(PHP 5 >= 5.3.0, PHP 7, PHP 8)
forward_static_call_array — static メソッドをコールし、引数を配列で渡す
callback
パラメータで指定したユーザー定義の関数あるいはメソッドをコールします。
この関数はメソッドのコンテキストでコールしなければなりません。
クラスの外部で使用することはできません。
この関数は 遅延静的束縛 を使います。
転送先のメソッドへのすべての引数は値渡しで、
call_user_func_array() と同様に配列で指定します。
callback
コールしたい関数あるいはメソッド。このパラメータは、 クラス名とメソッド名を指定した配列あるいは関数名を指定した文字列となります。
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
Regarding namespaces:Be sure to include fully namespaced class path:<?phpforward_static_call_array( array('NAMESPACE\CLASS_NAME', 'STATIC_METHOD'), $params);
one of academic example, when forward_static_call() can be useful<?phpclass 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());