title: Dynamically load class for global access (like codeigniter does)
body: <p>Here is the basics of my framework:</p>
  
  <pre><code>class CPU {
  
      public function load_class($class) {
          include_once($class . ".php");
          $this-&gt;$class = new $class;
      }
  
      public function load_controller($class) {
          include_once($class . ".php");
          $class = new $class;
          $class-&gt;index();
      }
  
      public function run() {
  
          // Load DB class
          $this-&gt;load_class("DB");
  
          // Load controller
          $this-&gt;load_controller("About");
      }
  }
  
  class About extends CPU {
      public function index() {
          $this-&gt;DB-&gt;connect();
      }
  }
  </code></pre>
  
  <p>When <code>run()</code> is called to load the <code>About</code> class, accessing <code>$DB</code> gives the error below:</p>
  
  <p><strong>Fatal error</strong>: Call to a member function connect() on a non-object</p>
  
  <p>I assume I need to use a singleton to create the class dynamically. CodeIgniter works in the same way but I can't work out what I have to do to amend my framework to make this work?</p>