作者热门文章
- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
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
只能从类方法中调用。
关于PHP forward_static_call 与 call_user_func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5061476/
forward_static_call 和 call_user_func 有什么区别? 同样的问题适用于 forward_static_call_array 和 call_user_func_arra
我是一名优秀的程序员,十分优秀!