Php __调用优先于_调用静态
我注意到下面的每个示例都会回显Php __调用优先于_调用静态,php,function,magic-methods,Php,Function,Magic Methods,我注意到下面的每个示例都会回显\u call class A { public function __construct() { $this->something(); self::something(); call_user_func(array("self", "something")); forward_static_call(array("self", "something")); A::som
\u call
class A {
public function __construct() {
$this->something();
self::something();
call_user_func(array("self", "something"));
forward_static_call(array("self", "something"));
A::something();
call_user_func(array(__CLASS__, "something"));
forward_static_call(array(__CLASS__, "something"));
}
public function __call($name, $arguments) {
echo __FUNCTION__ . "<br />";
}
public static function __callStatic($name, $arguments) {
echo __FUNCTION__ . "<br />";
}
}
new A();
A类{
公共函数构造(){
$this->something();
self::某物();
调用用户函数(数组(“self”,“something”);
前向静态调用(数组(“self”、“something”);
A::某物();
调用用户函数(数组(uuu CLASS_uuu,“something”);
前向静态调用(数组(uuuu CLASS_uuuu,“某物”);
}
公共函数调用($name,$arguments){
回声函数;
}
公共静态函数\uuu callStatic($name,$arguments){
回声函数;
}
}
新A();
即使类中存在\u调用
魔术方法,也可以从对象上下文内部调用\u调用static
方法吗
我发现call_user_func(数组(uuu CLASS_uuu,“uuu callStatic”)、数组(“方法”)、数组()
有点难看。\uu callStatic()
在静态上下文中调用不可访问的方法时触发
但是当您从非静态上下文中执行foo::bar()
(在您的代码中,\uu构造方法是非静态上下文)时,将是非静态调用,除非函数明确定义为静态
因此,如果您真的想在\uu构造
方法中使用\uu callStatic()
,您可以直接使用它
public function __construct() {
self::__callStatic('something', array());
}
如果调用是在静态方法中进行的,则将使用\u callStatic
。例如(至少对于某些版本)@Yoshi它是在对象上下文中生成的。uu callStatic通常有助于处理未标记的方法,那么为什么再次调用呢?