So I have been programming for a while and decided to learn about OOP. I made this class for a database connection and I am unsure if this is a waste of code or a good way to do things. I still don't feel like I understand OOP that much but I will get there it's just practice. I guess my aim really was to keep as much of the database connection private to the class as I could and also have the class do all the cleanup like mysqli_close();.
class db {
private $db_user;
private $db_pass;
private $db_host;
private $db_name;
private $link;
private $db_error;
public function escape($string) {
return mysqli_real_escape_string($this->link, $string);
}
public function query($query) {
return = mysqli_query($this->link, $query);
}
function __construct() {
$this->db_error = 'Database Error';
$this->db_user = 'root';
$this->db_pass = '';
$this->db_host = 'localhost';
$this->db_name = 'test';
$this->link = mysqli_connect($this->db_host, $this->db_user, $this->db_pass) or die($this->db_error);
mysqli_select_db($this->link, $this->db_name) or die($this->db_error);
}
function __destruct() {
mysqli_close($this->link);
}
}
Edit: Thanks for the answers I am going to learn PDO.