PHP允許使用的靜態成員函數和變量,因爲5.3包括延遲靜態綁定:在PHP中使用靜態成員是否有最佳做法?
class StaticClass {
public static $staticVar;
...
}
$o = new StaticClass();
目前,有訪問這些靜態成員的各種選項:
$o->staticVar; // as instance variable/ function
$o::staticVar; // as class variable/ function
其他選項用於訪問存在來自課堂的成員:
self::$staticVar; // explicitly showing static usage of variable/ function
static::$staticVar; // allowing late static binding
重構某些使某些靜態成員使用的現有類我問自己是否有在PHP中使用靜態成員的最佳實踐?
不要使用'$ o-> staticVar;'來訪問靜態屬性,因爲它最近會發出警告;使用類外的'StaticClass :: staticVar;' –