在PHP手冊它指出Scope Resolution Operator(::)有以下目的使用::爲非靜態方法?
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.
現在我只是碰到一個教程來了,是用雙冒號是這樣的:
class A {
public function nice(){
echo "hi";
}
}
$A = new A;
A::nice();
和輸出實際上是
但爲什麼這不會引發錯誤?函數nice
不是一個靜態方法,也不是重寫該方法。像這樣使用雙冒號是不好的做法嗎?
那是因爲你沒有啓用錯誤報告([DEMO](https://3v4l.org/5DVZI) )。恐怕有很多垃圾學習材料。 –
反應還取決於PHP70中的PHP版本:「棄用:非靜態方法foo :: bar()不應該在' – JustOnUnderMillions
@ÁlvaroGonzález中靜態調用。我實際上在sitepoint的書PHP Master中發現了這個問題。 – Adam