title: How to get instances into a base class
body: <p>I'm building a simple MVC framework in PHP and I'm stuck at the part where I have to create a <code>BaseController</code> class.</p>
  
  <p>Every "page" controller needs to extend from this <code>BaseController</code>. Because this BaseController class will have properties that will give the user access to a <code>template</code> engine and a <code>Logger</code> class (and some other things).</p>
  
  <p>The problem I have is that I'm not sure how to instantiate those things in the BaseController class. I can obviously hard code it in the <code>__constructo()</code> like this:</p>
  
  <pre><code>class BaseController
  {
      protected $view;
      protected $log;
  
      public function __construct()
      {
          $this-&gt;view = new \namespace\view('param1', 'param2');
          $this-&gt;log = new Logger('name');
          $this-&gt;log-&gt;pushHandler(new StreamHandler('path/to/your.log', Logger::WARNING));
      }
  }
  </code></pre>
  
  <hr>
  
  <p>But this doesn't make it very modular. It makes it hard to change the Logger class for example. Or if the user wants to change the template engine to Smarty for example.</p>
  
  <p>I also can't really use a form of Dependancy Injection. Because then every controller that extends from the <code>BaseController</code> will have to pass those instances to the <code>BaseController</code>. </p>
  
  <p>That would look something like this:</p>
  
  <pre><code>class BaseController
  {
      protected $view;
      protected $log;
  
      public function __construct($view, $log)
      {
          $this-&gt;view = $view;
          $this-&gt;log = $log;
      }
  }
  </code></pre>
  
  <p>HomeController</p>
  
  <pre><code>class HomeController extends BaseController
  {
      public function __construct($view, $log)
      {
          parent::__construct($view, $log);
  
          // Do my own stuff
      }
  }
  </code></pre>
  
  <p>To me that's not really user friendly when a user only wants to do a simple thing in the <code>__constructor</code>. Not even with an <code>IoC</code>.</p>
  
  <p>So the only thing that I can think of now is to use a <code>Service Provider</code> from the <code>__construct</code> method in the <code>BaseController</code>. But I'm not really sure if that's the way to go? Maybe there is a better alternative?</p>
  
  <p>So what would be a good solution to solve this problem? </p>
  
  <p>PS. And if I do need a <code>Service Locator</code>, are there any <strong><em>good</em></strong> examples out there? The ones I've found were written by people that seemed like they didn't really know what they were talking about. They were basically copying each others blog posts.</p>