title: PHP - How to access pdo object from other (multiple) classes
body: <p>I am switching my MVC to use PDO (I know, overdue). My application has, in the past, used the following class hierarchy: </p>
  
  <p>Database.class>Main.class>User.class </p>
  
  <p>(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).</p>
  
  <p>Using PDO, I've tried to imitate the process but find errors. I created a singleton function in the Database.class:</p>
  
  <pre><code>function __construct()
  {
      $this-&gt;db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PASSWORD);
      $this-&gt;db-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $this-&gt;db-&gt;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-&gt;db-&gt;prepare('SELECT * FROM users WHERE userID = :userID');
      $qry-&gt;execute(array(':userID' =&gt; 1));
      $results = $qry-&gt;fetchAll(PDO::FETCH_ASSOC);
      return $results;
  }
  </code></pre>
  
  <p>Then in Main.class I get the instance:</p>
  
  <pre><code>function __construct()
  {
      $this-&gt;db = parent::getInstance();
  }
  </code></pre>
  
  <p>So in User.class I try to call </p>
  
  <pre><code>function __construct(){
      parent::__construct();
  }
  function test(){
      return $this-&gt;db-&gt;query("test");
  }
  </code></pre>
  
  <p>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)</p>
  
  <p>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)</p>
  
  <p>I've seen people using injection for this, and I've seen examples of extending the pdo wrapper class (but only once). </p>
  
  <p>Thanks! (I love stack overflow!)</p>