title: PHP MVC Framework Structure
body: <p>I am sorry about the amount of code here.  I have tried to show enough for understanding while avoiding confusion (I hope).  I have included a second copy of the code at <a href="http://pastebin.com/86K4fAAH" rel="nofollow noreferrer">Pastebin</a>.  (The code does execute without error/notice/warning.)</p>
  
  <p>I am currently creating a Content Management System while trying to implement the idea of Model View Controller.  I have only recently come across the concept of MVC (within the last week) and trying to implement this into my current project.</p>
  
  <p>One of the features of the CMS is dynamic/customisable menu areas and each feature will be represented by a controller. Therefore there will be multiple versions of the Controller Class, each with specific extended functionality.</p>
  
  <p>I have looked at a number of tutorials and read some open source solutions to the MVC Framework.  I am now trying to create a lightweight solution for my specific requirements.  I am not interested in backwards compatibility, I am using PHP 5.3.  An advantage of the Base class is not having to use <code>global</code> and can directly access any loaded class using <code>$this-&gt;Obj['ClassName']-&gt;property/function();</code>.</p>
  
  <p>Hoping to get some feedback using the basic structure outlined (with performance in mind).  Specifically;<br>
  a) Have I understood/implemented the concept of MVC correctly?<br>
  b) Have I understood/implemented Object Orientated techniques with PHP 5 correctly?<br>
  c) Should the class propertise of Base be static?<br>
  d) Improvements?  </p>
  
  <p>Thank you very much in advance!</p>
  
  <pre><code>&lt;?php
  error_reporting(E_ALL);
  ini_set("display_errors", 1);
  
  /* A "Super Class" that creates instances */
  class Base {
  
      public static $Obj = array(); // Not sure this is the correct use of the "static" keyword?    
      public static $var;
  
      static public function load_class($directory, $class)
      {
          echo count(self::$Obj)."\n"; // This does show the array is getting updated and not creating a new array :)
          if (!in_array($class, self::$Obj))  //dont want to load it twice
          {
              /* Locate and include the class file based upon name ($class) */
              return self::$Obj[$class] = new $class();
          }
          return TRUE;
      }
  }
  
  /* Loads general configuration objects into the "Super Class" */
  class Libraries extends Base { 
      public function __construct(){
          $this-&gt;load_class('library', 'Database');
          $this-&gt;load_class('library', 'Session');
          self::$var = 'Hello World!';    //testing visibility
          /* Other general funciton classes */
      }
  }
  
  class Database extends Base {
      /* Connects to the the database and executes all queries */
      public function query(){}
  }
  
  class Session extends Base {
      /* Implements Sessions in database (read/write) */
  }
  
  /* General functionality of controllers */
  abstract class Controller extends Base {
      protected function load_model($class, $method) {
          /* Locate and include the model file */
          $this-&gt;load_class('model', $class);
          call_user_func(array(self::$Obj[$class], $method));
      }
      protected function load_view($name) {
          /* Locate and include the view file */
          #include('views/'.$name.'.php');
      }
  }
  abstract class View extends Base { /* ... */ }
  abstract class Model extends Base { /* ... */  }
  
  class News extends Controller {
      public function index() {
          /* Displays the 5 most recent News articles and displays with Content Area */
          $this-&gt;load_model('NewsModel', 'index');
          $this-&gt;load_view('news', 'index');
          echo self::$var;
      }
  
      public function menu() {
          /* Displays the News Title of the 5 most recent News articles and displays within the Menu Area */
          $this-&gt;load_model('news/index');
          $this-&gt;load_view('news/index');
      }
  }
  class ChatBox extends Controller { /* ... */  }
  /* Lots of different features extending the controller/view/model class depending upon request and layout */
  
  class NewsModel extends Model {
      public function index() {
          echo self::$var;
          self::$Obj['Database']-&gt;query(/*SELECT 5 most recent news articles*/);
      }
  
      public function menu() { /* ... */  }
  }
  
  
  $Libraries = new Libraries;
  
  $controller = 'News'; // Would be determined from Query String
  $method = 'index'; // Would be determined from Query String
  
  $Content = $Libraries-&gt;load_class('controller', $controller); //create the controller for the specific page
  
  if (in_array($method, get_class_methods($Content)))
  {
      call_user_func(array($Content, $method));
  }
  else
  {
      die('Bad Request'. $method);
  }
  
  $Content::$var = 'Goodbye World';
  echo $Libraries::$var . ' - ' . $Content::$var;
  ?&gt;
  
  /* Output */
  0
  1
  2
  3
  Hello World!Hello World!Goodbye World - Goodbye World
  </code></pre>