static 關鍵字定義類層級的屬性與方法,屬於類而非實例。本文系統講解靜態成員訪問規則、記憶體模型、self:: 與 static:: 差異、靜態方法調用非靜態成員的限制,並給出實戰最佳實踐。
$this,只能訪問靜態成員;ClassName::$property、ClassName::method()、類內部用 self:: 或 static::。class UserCounter {
private static int $count = 0;
public function __construct() {
self::$count++;
}
public static function getCount(): int {
return self::$count;
}
public function __destruct() {
self::$count--;
}
}
$user1 = new UserCounter();
$user2 = new UserCounter();
echo UserCounter::getCount(); // 2
unset($user1);
echo UserCounter::getCount(); // 1
特點:
UserCounter::$count(若為 public);static:: 延遲綁定)。class Math {
public static float $pi = 3.14159;
public static function circleArea(float $r): float {
return self::$pi * $r * $r;
}
public static function max(float ...$nums): float {
return max($nums);
}
}
echo Math::circleArea(3); // 28.27431
echo Math::max(1, 5, 3); // 5
class ParentClass {
public static string $name = 'Parent';
public static function whoami(): void {
echo self::$name . PHP_EOL; // 編譯期綁定 -> Parent
echo static::$name . PHP_EOL; // 運行期綁定 -> 實際調用類
}
}
class ChildClass extends ParentClass {
public static string $name = 'Child';
}
ParentClass::whoami(); // Parent / Parent
ChildClass::whoami(); // Parent / Child ← 關鍵差異
self:: 綁定定義該方法的類(編譯期);static:: 綁定實際調用的類(運行期),配合繼承多態;static::。| 場景 | 結果 | 原因 |
|---|---|---|
| 靜態方法調用非靜態屬性 | Fatal Error | 無 $this 上下文 |
| 靜態方法用 $this->prop | Fatal Error | 同上 |
| 靜態方法調用非靜態方法 | Fatal Error (PHP 7+) | 非靜態方法被視為靜態調用,內部若用 $this 再報錯 |
| 非靜態方法用 self:: 調用 | 自動降級為靜態調用 | 兼容歷史代碼,但會觸發 Deprecated 警告 |
const 而非 static:const 編譯期不可變,語義更清晰;readonly 修飾靜態屬性:初始化後不可變。// 1. 配置類:只讀靜態屬性 + 延遲初始化
final class AppConfig {
private static ?array $config = null;
public static function get(string $key, $default = null) {
self::init();
return self::$config[$key] ?? $default;
}
private static function init(): void {
if (self::$config === null) {
self::$config = require __DIR__ . '/config.php';
}
}
}
// 2. 工具類:純靜態方法,無狀態
final class Str {
public static function slug(string $str): string {
return strtolower(trim(preg_replace('/[^a-z0-9]+/i', '-', $str), '-'));
}
}
// 3. 計數器:私有靜態屬性 + 靜態訪問器
class RequestCounter {
private static int $count = 0;
public static function increment(): void { self::$count++; }
public static function get(): int { return self::$count; }
}
理解 static 的記憶體語義與繼承行為,能設計出高內聚、低耦合的類結構。