I'm new to using PDO and am trying to create a secure class to handle MySQL statements. Here is what I have so far:
class SQL {
private static $dbh = null;
private function __construct($host, $user, $pass, $data, $table) {
$this->host = (!empty($host)) ? $host: 'localhost';
$this->user = $user;
$this->pass = $pass;
$this->data = $data;
$this->table = $table;
static::connect();
}
private function __destruct() {
static::close();
}
public static function connect() {
$dsn = 'mysql:dbname=' . $this->table . ';host=' . $this->host;
static::$dbh = new PDO($dsn, $this->user, $this->pass);
static::$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
public static function close() {
static::$dbh = null;
}
public static function query($query) {
$sth = static::$dbh->prepare($query);
$sth->execute();
}
}
Is what I've done correct so far and conform to best practices?