I want to use in my application PDO connection within multiple classes (objects) but the way how I tried in the past was not correct because everytime I called an object I created a new PDO object (connection) also.
Im right now using a spl_autoload_register for autoloading.
Here Is my DataBase class (I deleted from this example the connection propeties (name, host etc etc.):
DataBase.php
namespace MyApp;
use PDO;
class DataBase {
public function DB_CONN() {
try {
$pdo = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->DB_name, $this->DB_username, $this->DB_passw, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
// Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
catch (PDOException $e) {
die("ERROR: Could not connect. " . $e->getMessage());
return false;
}
}
}
So in this example here I need a PDO connection to Customer class and I created a PDO object in the Customer constructor like this:
Customer.php
namespace MyApp;
use MyAppDataBase;
use PDO;
class Customer
{
private $PDO;
public function __construct($cus_id)
{
$this->PDO = (new DataBase)->DB_CONN();
}
public function getCustomerOrders(int $id): array
{
$CustomerOrders = (new Orders())->getOrdersByID($id)
}
public function getCustomerNameByID(int $id): ?string
{
$stmt = $this->PDO->prepare("SELECT name FROM customers WHERE id=:id");
$stmt->execute([":id" => $id]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result["name"];
}
}
But its not the best method because because if I need an another class in this example named Orders (which also needs a DataBase connection) then I will create an another PDO object.
Orders.php
namespace MyApp;
use MyAppDataBase;
use PDO;
class Orders
{
private $PDO;
public function __construct($cus_id)
{
$this->PDO = (new DataBase)->DB_CONN();
}
public function getOrdersByID(int $id): array
{
$stmt = $this->PDO->prepare("SELECT * FROM orders WHERE id =:id");
$stmt->execute([":id" => $id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
So my question is that should I only pass the $PDO variable to the given class constructor? It will solve my problem? Like this:
try {
$pdo = new PDO("mysql:host=" . $db_host . ";dbname=" . $db_name, $db_username, $db_passw, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
// Set the PDO error mode to exception
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
die("ERROR: Could not connect. " . $e->getMessage());
return false;
}
class Customer
{
private $PDO;
public function __construct(PDO$PDO)
{
$this->PDO = $PDO;
}
public function getCustomerOrders(int $id)
{
$CustomerOrders = (new Orders($this->PDO))->getOrdersByID($id);
}
public function getCustomerNameByID(int $id): ?string
{
$stmt = $this->PDO->prepare("SELECT name FROM customers WHERE id=:id");
$stmt->execute([":id" => $id]);
$result = $stmt->fetch(PDO::FETCH_ASSOC);
return $result["name"];
}
}
class Orders
{
private $PDO;
public function __construct(PDO$PDO)
{
$this->PDO = $PDO;
}
public function getOrdersByID(): array
{
$stmt = $this->PDO->prepare("SELECT * FROM ORDERS WHERE ID =:id");
$stmt->execute([":id" => $id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}
$Customer = new Customer($pdo);
print_r($Customer->getCustomerNameByID());
print_r($Customer->getCustomerOrders());
Or should I use dependency injection? Thank you!