Do not make it overcomplicated, just extend MySQLi
class, add some methods to it. MySQLi
is already a connector itself, why store another connector inside a connector?
class Database extends MySQLi {
private static $instance = null ;
private function __construct($host, $user, $password, $database){
parent::__construct($host, $user, $password, $database);
}
public static function getInstance(){
if (self::$instance == null){
self::$instance = new self(HOST, USER, PASSWORD, DATABASE);
}
return self::$instance ;
}
}
$db = Database::getInstance();
$result = $db->query("SELECT 1 FROM table");
A few tips:
Non-static
properties are accessed via$object->property
Static
properties are accessed via:self::$property
orDatabase::$property
orstatic::$property
- use of
PDO
is really good since you can bind values, thus securing your database from injections. As an option - extendPDO
class.