PHP - How to access pdo object from other (multiple) classes
I am switching my MVC to use PDO (I know, overdue). My application has, in
the past, used the following class hierarchy:
Database.class>Main.class>User.class
(each one extending the other). But before any object is created, the
mysql connection was made (mysql_connect). Once the connection was open I
could use Database.class as a wrapper class through which all my queries
were performed. Through extention, a query in the User.class could be made
simply by calling the "query" function ($this->query).
Using PDO, I've tried to imitate the process but find errors. I created a
singleton function in the Database.class:
function __construct()
{
$this->db = new
PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER,
DB_PASSWORD);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
public static function getInstance()
{
if (!isset(self::$instance)){
$object = __CLASS__;
self::$instance = new $object;
}
return self::$instance;
}
function query($qry,$params=NULL){
$qry = $this->db->prepare('SELECT * FROM users WHERE userID = :userID');
$qry->execute(array(':userID' => 1));
$results = $qry->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
Then in Main.class I get the instance:
function __construct()
{
$this->db = parent::getInstance();
}
So in User.class I try to call
function __construct(){
parent::__construct();
}
function test(){
return $this->db->query("test");
}
So I can run any queries fine from the Main.class object. But if I try to
run queries from User.class object I get the error: "Call to a member
function query() on a non-object" In other words, if User.class extends
main I should be able to access the variable $db in Main from User.class
(I call the constructor for Main when the User object is created). Part of
the issue is that Main.class is created earlier in the application as it's
own object, I believe causing two instances of PDO to be created - which
is why it doesn't work through extension (through a second object that
also extends the database.class)
So my question is: is there a way to make this happen? Or is my best
option to use injection for every object I create (because some scripts
incorporate multiple objects that extend Main.class - which try to create
an instance of PDO each time) and pass the pdo object to the constructor?
I'd rather not have to do that (the less markup the better) So another
option would be to use a STATIC variable that all classes use? What's the
best method? (let me know if this is confusing)
I've seen people using injection for this, and I've seen examples of
extending the pdo wrapper class (but only once).
Thanks! (I love stack overflow!)
No comments:
Post a Comment