gpt4 book ai didi

PHP forward_static_call 与 call_user_func

转载 作者:IT王子 更新时间:2023-10-29 00:18:07 25 4
gpt4 key购买 nike

forward_static_callcall_user_func 有什么区别?

同样的问题适用于 forward_static_call_arraycall_user_func_array

最佳答案

不同之处在于,forward_static_call 不会重置“被调用的类”信息,如果在类层次结构中上升并显式命名一个类,而 call_user_func 会重置信息这些情况(但如果使用 parentstaticself 仍然不会重置它)。

例子:

<?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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com