title: Initiate Class On Demand
body: <p>I am trying to initiate a class on demand.</p>
  
  <pre><code>class MyClass{
  
      private $modules = array("mod1" =&gt; false);
  
      public function __get($name){
          if(array_key_exists($name, $this-&gt;modules) &amp;&amp; !$this-&gt;modules[$name]){
              $class       = ucfirst($name);
              $this-&gt;$name = new $class();
              $this-&gt;modules[$name] = true;
          }
      }
  }
  </code></pre>
  
  <p>I then have another class, which then extends the above class. If I do the following the class doesn't get initiated, and I get an error <code>Fatal error: Call to a member function get() on a non-object</code></p>
  
  <pre><code>class Home extends MyClass{
  
      public function main(){
          echo $this-&gt;mod1-&gt;get("cat");
      }
  
  }
  </code></pre>
  
  <p>But if I do this, the class does get initiated.</p>
  
  <pre><code>class Home extends MyClass{
  
      public function main(){
          $this-&gt;mod1;
          echo $this-&gt;mod1-&gt;get("cat");
      }
  
  }
  </code></pre>
  
  <p>Is there any way for me to initiate the class without having to add that extra line?</p>