title: PHP objects and shared database connection
body: <p>What is better way for db connections in relevant instance, when using OOP approch in PHP?</p>
  
  <ul>
  <li>db connection passed to all instances </li>
  <li>each instance to have its own db connection? </li>
  </ul>
  
  <p>Here is my code example.</p>
  
  <p>Passing connection to constructor:</p>
  
  <pre><code>&lt;?php
  
  class Model {
  
    public $db;
  
    public function __construct($db) {
          $this-&gt;db = $db;
  
    }
  
  }
  </code></pre>
  
  <p>Connecting to db in model constructor.
      
  
  <pre><code>class Model {
  
    protected $db;
  
    public function __construct() {
  
          $this-&gt;db = new Detabase_Con();
  
    }
  
  }
  </code></pre>
  
  <p>Users model</p>
  
  <pre><code>class Users extends Model {
  
    public function showUsers() {
  
      $this-&gt;db-&gt;select("SELECT * FROM users");
  
    }
  
  }
  </code></pre>