title: PHP5 OOP Class Structure
body: <p>I know there are loads of questions on this, I have done quite a bit of reading. I'd like to ask this in context of my project to see what suggestions you may have.</p>
  
  <p>I have quite a large web application with many classes, e.g. <em>users</em> and <em>articles</em> (which i consider to be the main classes) and smaller classes such as <em>images</em> and <em>comments</em>. Now on a page, lets say for example an <em>article</em>, it could contain many instances of <em>images</em> and <em>comments</em>. Makes sense right? Now on say an <em>articles</em> page I call a static method which returns an array of <em>article</em> objects.</p>
  
  <p>That's the background, so here are the questions.</p>
  
  <p>Since building a large amount of the app I came to realise it would be very useful to have a core system class containing settings and shared functions. There for I extended all of my classes with a new core class. Seemed relatively simple and quick to implement. I know CodeIgniter does something similar. I feel now though my app is becoming a bit messy.</p>
  
  <p><strong>Question</strong> Is this a good idea? Creating an instance of core is exactly what I want when calling an instance of an article, but what about when i'm creating multiple instances using the static method, or calling multiple images or comments on a page. I'm calling the core class unnecessarily right? Really it only needs to be called once per page (for example the constructor defines various settings from the database, I don't want to this every time, only once per page obviously), but all instances of all classes should have access to that core class. Sounds exactly like I want the singleton approach, but I know that's a waste of time in PHP.</p>
  
  <p>Here's an <strong>idea</strong> of what my code looks like at this point. I've tried to keep it as simple as I can.</p>
  
  <pre><code>class core {
  
      public function __construct(){
          ...define some settings which are retrieve from the database
      }
  
      public function usefulFunction(){
      }
  }
  
  
  class user extends core {
  
      public function __construct(){
          parent::__construct();
      }
  
      public function getUser($user_id){
          $db = new database();
          $user = /* Get user in assoc array from db */
  
          $this-&gt;__setAll($user);
      }
  
      public static function getUsers(){
          $db = new database();
          $users = /* Get users from database in assoc array from db */
  
          foreach($users as $user) {
               $arrUsers[] = new self();
               $arrUsers[]-&gt;__setAll($user);
          }
  
          return $arrUsers;
      }
  
      private function __setAll($attributes) {
          foreach($attributes as $key =&gt; $value)
          {
              $this-&gt;__set($key, $value);
          }   
      }
  
      public function __set($key, $value) {
           $this-&gt;$key = $value;
      }
  
  }
  </code></pre>
  
  <p>The other issue I'm having is efficiently using/sharing a database connection. Currently each method in a class requiring a database connection creates a new instance of the database, so on a page I might be doing this 5 or 10 times. Something like the dependency injection principle sounds much better.</p>
  
  <p><strong>Question</strong> Now if i'm passing the instance of the DB into the new user class, i know I need something like this...</p>
  
  <pre><code>class user{
      protected $db;
  
      public function __construct($db){
          $this-&gt;db = $db;
      }
  
      ... etc
  }
  
  $db = new database();
  $user = new user($db);
  </code></pre>
  
  <p>... but when I want to run the static function users::getUsers() what is the best way to gain access to the database instance? Do i need to pass it as a variable in each static method? (there are many static methods in many classes). It doesn't seem like the best way of doing it but maybe there isn't another way.</p>
  
  <p><strong>Question</strong> If extending all of my classes off the core class as suggested in part 1, can I create an instance of the DB there and access that some how?</p>
  
  <p><strong>Question</strong> I also have various files containing functions (not oop) which are like helper files. What's the best way for these to access the database? Again i've been creating a new instance in each function. I don't really want to pass the db as a parameter to each one. Should I use globals, turn these helper files into classes and use dependency injection or something different all together?</p>
  
  <p>I know there is lots of advice out there, but most info and tutorials on PHP are out of date and don't ever seem to cover something this complex...if you can call it complex?</p>
  
  <p>Any suggestions on how to best layout my class structure. I know this seems like a lot, but surely this is something most developers face everyday. If you need any more info just let me know and thanks for reading!</p>