title: Create database model classes dynamically
body: <p>I am trying to improve the method that I am using to to database transactions in a light framework I've built.</p>
  
  <h2>Information to understand the question:</h2>
  
  <p>Here's a class I've written (where <code>connect.php</code> loads up database credentials; a wrapper for the PHP PDO, stored in <code>$db</code>; and <code>Base.php</code>):    </p>
  
  <pre><code>&lt;?php
      require_once('connect.php');
  
      class Advertiser extends Base
      {
          public static function getByID($id)
          {
              global $db;
              $sql = "SELECT * FROM advertiser WHERE advertiserid=?";
              $values = array($id);
              $res = $db-&gt;qwv($sql, $values);
  
              return Advertiser::wrap($res);
          }
  
          public static function add($name)
          {
              $adv = new Advertiser(null, $name);
              $res = $adv-&gt;save();
  
              return $res;
          }
  
          public static function wrap($advs)
          {
              $advList = array();
              foreach( $advs as $adv )
              {
                  array_push($advList, new Advertiser($adv['advertiserid'], $adv['name']));
              }
  
              return Advertiser::sendback($advList);
          }
  
          private $advertiserid;
          private $name;
  
          public function __construct($advertiserid, $name)
          {
              $this-&gt;advertiserid = $advertiserid;
              $this-&gt;name = $name;
          }
  
          public function __get($var)
          {
              return $this-&gt;$var;         
          }
  
          public function save()
          {
              global $db;
              if( !isset($this-&gt;advertiserid) )
              {
                  $sql = "INSERT INTO advertisers (name) VALUES(?)";
                  $values = array($this-&gt;name);
                  $db-&gt;qwv($sql, $values);
  
                  if( $db-&gt;stat() )
                  {
                      $this-&gt;advertiserid = $db-&gt;last();
                      return $this;
                  }
                  else
                  {
                      return false;
                  }
              }
              else
              {
                  $sql = "UPDATE advertisers SET name=? WHERE advertiserid=?";
                  $values = array ($this-&gt;name, $this-&gt;advertiserid);
                  $db-&gt;qwv($sql, $values);
  
                  return $db-&gt;stat();
              }
          }
      }
  ?&gt;
  </code></pre>
  
  <p>As you can see, it has fairly standard <a href="http://en.wikipedia.org/wiki/Create,_read,_update_and_delete" rel="nofollow">CRUD</a> functions (<strong>Edit</strong>: Okay, so only CRU, in this implementation).  Sometimes, I'll extend a class like this by adding more functions, which is what these classes are intended for.  For example, I might add the following function to this class (assuming I add a column <code>isBanned</code> to the database):  </p>
  
  <pre><code>public static function getBanned()
  {
      global $db;
      $sql = "SELECT * FROM advertiser WHERE isBanned=1";
      $res = $db-&gt;q($sql);
  
      return Advertiser::wrap($res);
  }
  </code></pre>
  
  <h2>The question:</h2>
  
  <p><strong>How can I create a catchall class that will also load up custom model classes when present and necessary?</strong></p>
  
  <p>For example, if I write the following code:</p>
  
  <pre><code>$model = new Catchall();
  $banned = $model-&gt;Advertiser::getByID(4);
  </code></pre>
  
  <p>I would expect my catchall class to modify its queries so that all the references to the tables/columns are whatever name I chose (<code>Advertiser</code>, in this case), but in lower case.</p>
  
  <p>In addition, if I wanted to create a custom function like the one I wrote above, I would expect my catchall class to determine that a file exists in its path (previously defined, of course) with the name that I've specified (<code>Advertisers.php</code>, in this case) and load it.</p>
  
  <p><code>Advertisers.php</code> would <code>extends Catchall</code> and would contain only my custom function.</p>
  
  <p>In this way, I could have a single Catchall class that would work for all CRUD functions, and be able to easily expand arbitrary classes as necessary.</p>
  
  <ol>
  <li><strong>What are the ideas / concepts that I need to understand to do this?</strong></li>
  <li><strong>Where can I find examples of this already in the wild, <em>without digging through a lot of CodeIgniter or Zend sourcecode</em>?</strong></li>
  <li><strong>What is what I'm trying to do <em>called</em>?</strong></li>
  </ol>