PHP:通过 ClassName::method 语法调用实例方法,导致静态调用? [英] PHP: call to an instance method via ClassName::method syntax, results in a static call?
问题描述
她是我的代码:
class MyClass
{
public $prop;
public function method ()
{
echo $this->prop;
}
}
然后在代码中的某个地方,意外地:
Then somewhere in the code, accidently:
MyClass::method();
我希望对上述行有一个解释错误,因为被调用的方法不是静态的.相反,该方法被调用,我收到了一个关于 $prop 不存在的异常.所以我知道该方法被称为静态方法,即使它不是.
I would expect to have an interpretation error about the above line, because the called method is not static. Instead, the method was called, and I received an exception about $prop not existing. So i understand that the method was called as a static method, even though it's not.
它是这样工作的吗?(为什么?)
Does it work this way? (Why the hell? )
推荐答案
静态调用非静态方法会生成 E_STRICT 级别警告.
Calling non-static methods statically generates an E_STRICT level warning.
http://php.net/manual/en/language.oop5.静态.php
我想您已经抑制了 E_STRICT 警告.它有效(可能是由于遗留原因),但不推荐这样做.
I suppose you have E_STRICT warnings suppressed. It works (likely for legacy reasons), but it's not recommended.
这篇关于PHP:通过 ClassName::method 语法调用实例方法,导致静态调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!