What's a better practice? To pass database or open database? I'm unable to do an auto-complete (NetBeans PHP IDE) when I pass a database into the constructor.
class Item {
private $name;
private $database;
public function __construct($database, $id) {
$information = $database->fetchSingleRow($id);
$this->database = $database;
$this->name = $information['name'];
}
}
Should I do this
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();
}
}