什麼是更好的做法?通過數據庫或打開數據庫?當我將數據庫傳遞給構造函數時,我無法執行自動完成(NetBeans PHP IDE)。將數據庫類傳遞給構造函數?
class Item {
private $name;
private $database;
public function __construct($database, $id) {
$information = $database->fetchSingleRow($id);
$this->database = $database;
$this->name = $information['name'];
}
}
我應該這樣做
class Item {
private $name;
private $database;
public function __construct($id) {
$this->database = new Database();
$this->database->open();
$this->database->select('test'); // selects test table
$information = $this->$database->fetchSingleRow($id);
$this->name = $information['name'];
}
public function __destruct() {
unset($this->item);
$this->database->close();
}
}