In my app I used one database only, and I created class to provide some common methods. Now I need to implement additional database.
So instead of creating separate Db class to do same things, I want to call that Db class with db related parameters.
Problem is, when I try to extend Db class with a class where I provide methods to app, database connection isn't established - it is null.
Both Main and User are using different databases.
Class Main extends Db; Class User extends Db;
Why something like this doesn't work?
class Db {
protected $link;
private $host;
private $user;
private $pass;
private $dbName;
public function __construct($host, $user, $pass, $db) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbName = $db;
$this->connect();
}
public function connect() {
try
{
$this->link = new PDO("mysql:host=" . $this->host .
";dbname=" . $this->dbName . ";charset=utf8mb4", $this->user, $this->pass );
$this->link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->link->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
} catch (PDOException $exc)
{
$this->setMessage($exc->getMessage());
}
}
}
$db = new Db(HOST, USER,PASS,DB)
class Main extends DB {
public function __construct() {
}
/** App methods bellow*/
}
$db2 = new Db(HOST2,USER2,PASS2,DB2);
class User extends DB {
public function __construct() {
}
/** App methods bellow*/
}
What would be easiest solution to implement two different database connections?
Thanks!
Edit:
After updating my code as suggested, I have issues with accessing connection property of DB, so I am not able to do queries in Main and User class.
What really confuses me, is the reason why that property it isn't visible to Main class?
Main class methods are depending heavily on db link, and I am not sure how to fix this..
Updated code:
class Main {
protected $db;
public function __construct($db) {
$this->db = $db;
}
public function showFreeSlotsPerPlan() {
try
{
$SQL = "some query";
$stmt = $this->db->prepare($SQL);
/** Prepared statements bellow **/
}
}
class Db {
protected $link;
private $host;
private $user;
private $pass;
private $dbName;
public function __construct($host, $user, $pass, $db) {
$this->host = $host;
$this->user = $user;
$this->pass = $pass;
$this->dbName = $db;
$this->connect();
}
}
And calling it as suggested:
$db = new Db(HOST, USER, PASS, DB);
$main = new Main($db);
And if I try to call a method f.e. showFreeSlotsPerPlan()
Fatal error: Call to undefined method Db::prepare() in /var/www/html/Master/class/Main.class.php
Then when I debug instance of Main I get the following:
Main Object
(
[db:protected] => Db Object
(
[link:protected] => PDO Object
(
)
[host:Db:private] => localhost
[user:Db:private] => root
[pass:Db:private] => password
[dbName:Db:private] => dbname
)
)
Edit2:
Workaround is adding prepare
method to the DB.php
, and I was able to execute query successfully.
Still I am not sure is this best aproach. I admit that my design is not good at all :(
public function prepare($sqlQuery) {
return $this->link->prepare($sqlQuery);
}