Fatal error: Using $this when not in object context in ... on line XX
在 `static` 靜態方法中使用了 `$this`。靜態方法屬於類,不屬於任何實例,因此沒有 `$this` 指針。
class DB {
private $link_id;
public static function _init(array $dsn) {
// 錯誤:靜態方法中調用 $this->link_id
if (!$this->link_id = @mysql_connect($dsn['host'], $dsn['user'], $dsn['pwd'])) {
// ...
}
}
}
class DB {
private $link_id;
public function __construct(array $dsn) {
$this->connect($dsn);
}
private function connect(array $dsn): void {
$this->link_id = @mysql_connect($dsn['host'], $dsn['user'], $dsn['pwd'])
?: throw new RuntimeException('DB connect failed');
}
// 靜態工廠方法返回實例
public static function create(array $dsn): self {
return new self($dsn);
}
}
// 使用
$db = DB::create($dsn);
class DB {
private static $link_id = null;
public static function init(array $dsn): void {
self::$link_id = @mysql_connect($dsn['host'], $dsn['user'], $dsn['pwd'])
?: throw new RuntimeException('DB connect failed');
}
public static function query(string $sql) {
return mysql_query($sql, self::$link_id);
}
}
缺點:全局單一連接,難以支持多數據庫、難以單元測試、非線程安全(PHP-FPM 多進程下各進程獨立,尚可接受)。
class DB {
private static ?self $instance = null;
private $link_id;
private function __construct(array $dsn) { /* connect */ }
public static function getInstance(array $dsn): self {
if (self::$instance === null) {
self::$instance = new self($dsn);
}
return self::$instance;
}
}
示例中使用的 `mysql_connect` 等函數自 PHP 5.5 棄用,PHP 7.0 移除。請改用 PDO 或 mysqli**:
// PDO 單例示例
class DB {
private static ?PDO $pdo = null;
public static function getPDO(array $dsn): PDO {
if (self::$pdo === null) {
$dsnStr = "mysql:host={$dsn['host']};dbname={$dsn['db']};charset={$dsn['charset']}";
self::$pdo = new PDO($dsnStr, $dsn['user'], $dsn['pwd'], [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
]);
}
return self::$pdo;
}
}