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