title: Passing data from Controller to View in a PHP MVC app
body: <p>In almost all tutorials or answers on SO, I see a common way to send data from a Controller to the View, the class View often looks something similar than the code below:</p>
  
  <pre><code>class View
  {
      protected $_file;
      protected $_data = array();
  
      public function __construct($file)
      {
          $this-&gt;_file = $file;
      }
  
      public function set($key, $value)
      {
          $this-&gt;_data[$key] = $value;
      }
  
      public function get($key) 
      {
          return $this-&gt;_data[$key];
      }
  
      public function output()
      {
          if (!file_exists($this-&gt;_file))
          {
              throw new Exception("Template " . $this-&gt;_file . " doesn't exist.");
          }
  
          extract($this-&gt;_data);
          ob_start();
          include($this-&gt;_file);
          $output = ob_get_contents();
          ob_end_clean();
          echo $output;
      }
  }
  </code></pre>
  
  <p>I don't understand why I need to put the data in an array and then call extract($this->_data).
  Why not just put directly some properties to the view from the controller like </p>
  
  <pre><code>$this-&gt;_view-&gt;title = 'hello world';
  </code></pre>
  
  <p>then in my layout or template file I could just do:</p>
  
  <pre><code>echo $this-&gt;title;
  </code></pre>