title: How to load a model in another controller using a custom PHP MVC
body: <p>My application have this folders stucture:</p>
  
  <blockquote>
    <p><ul>
    <li><strong>application</strong> </p>
    
    <blockquote>
      <p><ul>
      <li><strong>library</strong></p>
      
      <blockquote>
        <ul>
        <li>Model.php</li>
        <li>Controller.php</li>
        <li>View.php</li>
        </ul>
      </blockquote></li>
      <li><strong>models</strong>
      
      <ul>
      <li><em>Settings_Model.php</em></li>
      <li><em>Home_Model.php</em></li>
      </ul>
    </blockquote>
    
    <blockquote>
      <ul>
      <li><strong>controllers</strong>
      
      <blockquote>
        <ul>
        <li><em>settings.php</em></li>
        <li><em>home.php</em></li>
        </ul>
      </blockquote></li>
      <li><strong>views</strong>
      
      <blockquote>
        <ul>
        <li><strong><em>settings</em></strong>
        
        <blockquote>
          <ul>
          <li><em>index.php</em></li>
          </ul>
        </blockquote></li>
        <li><strong><em>home</em></strong>
        
        <blockquote>
          <ul>
          <li><em>index.php</em></li>
          </ul>
        </blockquote></li>
        </ul>
      </blockquote></li>
      </ul>
    </blockquote></li>
    <li>index.php</li>
    <li>.htaccess</li>
    </ul></li>
    </ul>
  </blockquote>
  
  <p><strong>library/Model.php</strong></p>
  
  <pre><code> class Database extends PDO{
          public function __construct($DB_TYPE, $DB_HOST, $DB_NAME, $DB_USER, $DB_PASS){
                parent::__construct($DB_TYPE.':host='.$DB_HOST.';dbname='.$DB_NAME, $DB_USER, $DB_PASS);       
          }
    }
  </code></pre>
  
  <p><strong>library/Controller</strong></p>
  
  <pre><code> class Controller{
  
        function __construct(){       
             $this-&gt;view = new View();
        }
  
        public function loadModel($name){
             $path = 'application/models/' . $name . '_model.php';
  
             if(file_exists($path)){
                  require 'application/models/' . $name . '_model.php';
                  $modelName = $name . '_Model';
                  $this-&gt;model = new $modelName();            
             }
        }
   }
  </code></pre>
  
  <p><strong>library/View</strong></p>
  
  <pre><code> class View{
  
        function __construct(){       
             $this-&gt;view = new View();
        }
  
        public function render($name){
             require 'application/views/' . $name . '.php';
        }
   }
  </code></pre>
  
  <p>In <strong>Models folder</strong> I created this model called <strong>Settings_Model.php</strong> : <em>models/Settings_Model.php</em>  </p>
  
  <pre><code> class Settings_Model extends Model{
      public function __construct(){parent::__construct();}
  
      function settings(){
         $stmt = $this-&gt;db-&gt;prepare("SELECT * FROM `mv_settings`");
         $stmt-&gt;execute();
         $stmt-&gt;setFetchMode(PDO::FETCH_OBJ);
         return $stmt-&gt;fetch();
      }       
   }
  </code></pre>
  
  <p>In <strong>Controllers folder</strong> I have this Controller called <strong>settings.php</strong> :
   <em>controllers/settings.php</em> </p>
  
  <pre><code>class Settings extends Controller {
  
      function __construct(){parent::__construct();}
  
      public function index(){    
          $this-&gt;view-&gt;settings = $this-&gt;model-&gt;settings();   
          $this-&gt;view-&gt;render('settings/index');      
      }   
  }
  </code></pre>
  
  <p>And In <strong>Views folder</strong> I created a folder called <strong>settings</strong> and inside this folder I created an <strong>index.php</strong> file:
   <em>settings/index</em></p>
  
  <p>and this is how to call the settings method inside settings/index:</p>
  
  <blockquote>
    <p><code>$this-&gt;settings-&gt;fieldName;</code></p>
  </blockquote>
  
  <p>So now when we go to <strong>Home</strong> view, I did the same procedure: 
   - create a Home_Model class and inside this model I created the same settings method:</p>
  
  <pre><code> class Home_Model extends Model{
      public function __construct(){parent::__construct();}
  
      function settings(){
         $stmt = $this-&gt;db-&gt;prepare("SELECT * FROM `mv_settings`");
         $stmt-&gt;execute();
         $stmt-&gt;setFetchMode(PDO::FETCH_OBJ);
         return $stmt-&gt;fetch();
      }       
   }
  </code></pre>
  
  <p>and then goes to the controllers folder and create <strong>Home</strong> controller:</p>
  
  <pre><code>class Home extends Controller {
  
      function __construct(){parent::__construct();}
  
      public function index(){    
          $this-&gt;view-&gt;settings = $this-&gt;model-&gt;settings();   
          $this-&gt;view-&gt;render('home/index');      
      }   
  }
  </code></pre>
  
  <p>and then call the settings method inside home/index:</p>
  
  <blockquote>
    <p><code>$this-&gt;settings-&gt;fieldName;</code></p>
  </blockquote>
  
  <p>What Im asking is how to avoid not to create the same method <em>settings</em> again and again each time we need to call that method in another view file???</p>
  
  <blockquote>
    <p><strong>NB</strong>: Im still a newbie.</p>
  </blockquote>