PHP forward_static_call 与 call_user_func
和 之间有什么区别forward_static_call
call_user_func
同样的问题也适用于和forward_static_call_array
call_user_func_array
和 之间有什么区别forward_static_call
call_user_func
同样的问题也适用于和forward_static_call_array
call_user_func_array
不同之处在于,如果向上移动类层次结构并显式命名类,则不会重置“被调用的类”信息,而在这些情况下会重置信息(但如果使用 或 ),则不会重置它)。forward_static_call
call_user_func
parent
static
self
例:
<?php
class A {
static function bar() { echo get_called_class(), "\n"; }
}
class B extends A {
static function foo() {
parent::bar(); //forwards static info, 'B'
call_user_func('parent::bar'); //forwarding, 'B'
call_user_func('static::bar'); //forwarding, 'B'
call_user_func('A::bar'); //non-forwarding, 'A'
forward_static_call('parent::bar'); //forwarding, 'B'
forward_static_call('A::bar'); //forwarding, 'B'
}
}
B::foo();
请注意,如果沿着类层次结构向下移动,则拒绝转发:forward_static_call
<?php
class A {
static function foo() {
forward_static_call('B::bar'); //non-forwarding, 'B'
}
}
class B extends A {
static function bar() { echo get_called_class(), "\n"; }
}
A::foo();
最后,请注意,只能从类方法中调用。forward_static_call