title: PHP MVC: Registry Gets Lost on Way to Controller
body: <p>I'm working with <a href="http://phpro.org/tutorials/Model-View-Controller-MVC.html" rel="nofollow">PHPRO's MVC framework</a> and am having problems passing a Registry object to my controller class.</p>
  
  <p>In my Router class, the loadController() method determines which controller to load and instantiates it. In the process, it passes the controller a Registry object that contains, among other things, a Template object:</p>
  
  <pre><code>class Router
  {
      private $registry;                // passed to Router's constructor
      public $file;                     // contains 'root/application/Index.php'
      public $controller;               // contains 'Index'
  
      public function loadController()
      {
          $this-&gt;getController();       // sets $this-&gt;file, $this-&gt;controller
          include $this-&gt;file;          // loads Index controller class definition
          $class = $this-&gt;controller;
          $controller = new $class($this-&gt;registry);
      }
  }
  </code></pre>
  
  <p>From Xdebug, I know that Router's $registry property has everything it's supposed to prior to being passed as an argument to Index's constructor.</p>
  
  <p>However, $registry fails to make it to Index intact. Here are the class definitions for Index and its parent Controller:</p>
  
  <pre><code>abstract class Controller
  {
      protected $registry;
  
      function __construct($registry)
      {
          $this-&gt;registry = $registry;
      }
      abstract function index();
  }
  
  class Index extends Controller
  {
      public function index()
      {
          $this-&gt;registry-&gt;template-&gt;welcome = 'Welcome';
          $this-&gt;registry-&gt;template-&gt;show('index');
      }
  }
  </code></pre>
  
  <p>With the code as shown, I get this error message: "Call to undefined method stdClass::show() in ...Index.php".</p>
  
  <p>Within Index, Xdebug shows $registry as null, so I know it's inheriting from the parent. But somewhere between the code to create a new Index object and the Index class definition, $registry gets lost.</p>
  
  <p>While debugging, I found that eliminating the Controller class from the equation stops the error from occurring:</p>
  
  <pre><code>class Index // extends Controller
  {
      private $registry;
  
      function __construct($registry)
      {
          $this-&gt;registry = $registry;
      }
  
      public function index()
      {
          $this-&gt;registry-&gt;template-&gt;welcome = 'Welcome';
          $this-&gt;registry-&gt;template-&gt;show('index');
      }
  }
  </code></pre>
  
  <p>Of course, this doesn't really solve anything because I still need the Controller class, but hopefully it will help as a clue to the problem.</p>
  
  <p>Can anyone see why I'm losing the contents of $registry when it gets passed to Index?</p>