I've build up new DB connection class for simple MVC pattern.
I need to know this is the correct way to do this.
<?php
include_once 'config.php';
class dbModel{
private $dbSys = "";
private $dbHost = "";
private $dbUser = "";
private $dbPass = "";
private $dbName = "";
private con = false;
public function __construct(){
$this->dbSys = DB_SYS;
$this->dbHost = DB_HOST;
$this->dbUser = DB_USER;
$this->dbPass = DB_PASS;
$this->dbName = DB_NAME;
if (!$this->con){
try{
$this->con = new PDO($this->dbSys.':host='.$this->dbHost.';dbname='.$this->dbName, $this->dbUser, $this->dbPass);
return $this->con;
} catch (PDOException $e){
echo $e->getMessage();
exit();
}
}else{
return $this->con;
}
}
}
?>
I included config.php file for database configuration as a separate file. I'm creating new object from this db connection class in other Model in my project and writing sql and run queries.
I tested this code and this is working.but I need to know this right way or not.
Please let me know this is correct way.