title: How should I pass a MySQL database link to a class in PHP?
body: <p>I'm trying to make a small CMS for fun but I'm getting caught up on some semantics with OOP. I have a basic database class and then I have an administrator class. The admin class has to make database queries, so what I've been doing is having the admin class extend the db class and then passing the link to the db resource via construct. My code looks something like this:</p>
  
  <pre><code>class admin extends mysql {
      function __construct($link) {
          $this-&gt;link = $link; //$this-&gt;link is in the mysql class
      }
  }
  $db = new mysql($host,$user,$pass,$name);
  $admin = new admin($db-&gt;link);
  </code></pre>
  
  <p>I know there should be an easier method to this without extending the db class (which I assume calls an entirely new instance of it). A friend told me to just pass the $db object to the admin class and store it there like so:</p>
  
  <pre><code>class admin {
      var $db;
      function __construct($db) {
          $this-&gt;db = $db;
      }
  }
  
  $db = new mysql($host,$user,$pass,$name);
  $admin = new admin($db);
  </code></pre>
  
  <p>Is this the correct way to do things? Is there an easier way to reference the db link if it's already been established?</p>