Is this a correct way to make PDO Connection.
I have different classes (each class have its own file) then there is config file contain PDO object and all classes objects. I am doing it correctly or there is any better practice.
classA.php
class classA {
private $PDO;
function __construct($PDO) {
$this->PDO = $PDO;
}
//other functions
}
classB.php
class classB {
private $PDO;
function __construct($PDO) {
$this->PDO = $PDO;
}
//other functions
}
classC.php
class classC {
private $PDO;
function __construct($PDO) {
$this->PDO = $PDO;
}
//other functions
}
And in config.php page:
include_once("db.php"); //contains db variables values
try
{
$PDO = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password, $options);
}
catch(PDOException $ex)
{
die("Failed to connect database: " . $ex->getMessage());
}
require 'classA.php';
require 'classB.php';
require 'classC.php';
$objA = new classA($PDO);
$objB = new classB($PDO);
$objC = new classC($PDO);
include config.php on almost every page.