PHP:MVC - Model View Controller App Class & Loader

With this tutorial, I’m laying the basics and beginning of my series on the MVC (Model View Controller) framework in PHP. Approximately, I estimated the number of tutorials to be well over 43. Might ‘compress’ some and merge what I currently have as a scheme. Till then, enjoy the introduction series starting off with this one.
Introduction:
Now a very plain scheme how an MVC framework works. First thing we have is the submitted data. That is what our browser sends when we try to access a page or service. They are then parsed to the controller which calls a specific model which we have previously coded. The model from its side, can call another model and another and so on. We are not limited to the amount of dependencies concerning the model connections. After that, depending on whether the data has been rendered as valid or not, the controller send the data to a view for visualization and interaction with the user.
Most plainly said, MVC is ‘model’ that separates the data visualization from the user’s interaction with it. We’re gonna look into the usage of MVC in web applications. As of its abbreviation, it consists of the following:
[] Model
- The model handles the business logic and managed the data. Let’s take for example a database that hold a number of records. The model is what connects to that database, collects the records, modifies them and determination of where the data is valid or not. If they are valid, where to write them and if not what not to do with them. The model simply validates the data.
[] View
- View represents and visualizes the data. The model takes the data from a database or whatever and the view outputs them. Most basically said, we need something that would convert those array or objects structures into HTML in most cases. It can format emails, server responses and more.
[*] Controller
- The controller determines which models to ‘absorb’ and send them to be processed through the view. Depending on the input data, it determines which models to be called, takes the data and re-sends to view.
That’s gonna be the structure we’re gonna follow through the process of creating the framework. We gonna start with our first class - App. We initiate the ‘program’ and end it with that class - takes the query, calls the front controller, define configurations, establish sessions and will kill the code in the end. Also before we start with it, edit the templates if you’re using Netbeans as an IDE or just simply remove the closing block tag for PHP code. A single space or new line after it can cause totally different output and mess our code immensely. When removing it, all the code till the last line is rendered as PHP. Another thing we’re going to use throughout the series is the declaration of names which will be conventional. This means that if our file is called App.php we gonna name the class within it with the same name. We’ll also define namespaces to ease our work.
<?php
namespace KF;
class App {
}
And because App can exist only once in the whole request which means that there could be only one instance during the execution of the code, this is the perfect place to use a singleton template. This will happen the following way:
<?php
namespace KF;
class App {
private static $_instance=null;
public static function getInstance(){
if (self::$_instance == null) {
self::$_instance = new \KF\App();
}
return self::$_instance;
}
}
It is not necessary to use the full path to the class because we could use ‘use’ with the namespace but I’m used to including the full path. Now something that is more than important is to specify the return type. This is what value or type of object will the specified method return. That’s mainly due to the fact that this will ease our work in the future development of the framework and that is why we need to define an instance to what it is going to return.
/**
*
* [@return](/profile/return) \KF\App
*/
Now we just need a public function that will actually run the application. Note where I’m declaring it:
<?php
namespace KF;
class App {
private static $_instance=null;
public function run(){
}
/**
*
* [@return](/profile/return) \KF\App
*/
public static function getInstance(){
if (self::$_instance == null) {
self::$_instance = new \KF\App();
}
return self::$_instance;
}
}
So far we’re done with the singleton template in the App class. Now let’s go back to our index.php and include the class to initiate the program.
<?php
include '../../KF/App.php';
$app = \KF\App::getInstance();
$app->run();
That’s be the initialization of the application class. And just to make sure everything is working as it should be, let’s echo out a string in the run() method in App.php:
<?php
namespace KF;
class App {
private static $_instance=null;
public function run(){
echo 'Devoted to Antagonism and Infamous';
}
/**
*
* [@return](/profile/return) \KF\App
*/
public static function getInstance(){
if (self::$_instance == null) {
self::$_instance = new \KF\App();
}
return self::$_instance;
}
}
Now let’s begin the coding of the class loader which will be responsible for including the classes that we attempt to use. We’re all aware that before we make an instance of a class, we need to include it of course. Whether through include, include_once, require or require_once, this can cause conflicts if we repeat the inclusion for the same class more than once or any recursive operation and so on. Here comes the time when we’ll see how this can be done without the usage of those functions.
<?php
namespace KF;
final class Loader {
private function __construct() {
}
}
First thing we gonna make is force the constructor to be private so as nobody would be able to create an instance for this class. This is one of the few places where we gonna force this but it’s vital and you’ll see the point of it in the future series. Now let’s move on:
<?php
namespace KF;
final class Loader {
private function __construct() {
}
}
Now we’ll use a function that will allow us to register our own system for loading when PHP can’t find the according class because when we make an instance of a class, PHP checks whether it is included and in case it is not, calls its own autoloader. Now whenever that autoloader fails, an error is output and further execution is terminated at that point.
<?php
namespace KF;
final class Loader {
private function __construct() {
}
public static function registerAutoLoad() {
spl_autoload_register(array("\KF\Loader", 'autoload'));
}
public static function audoload($class) {
self::loadClass($class);
}
}
spl_autoload_register() expects a call-back function which means a function to be called when PHP stumbles upon this situation. In this case when we want to pass a call-back function as an object we supply an array as the first parameter and the first element of that array is the object itself or rather the instance of that object (most commonly as I came to see through my experience it’s $this which is the name of the object that needs to be called).
Let’s take an example to make it clearer. Let’s see what the $class parameter for autoload() expects:
<?php
namespace KF;
final class Loader {
private function __construct() {
}
public static function registerAutoLoad() {
spl_autoload_register(array("\KF\Loader", 'autoload'));
}
public static function audoload($class) {
echo $class;
}
}
And now let’s make an instance of a non existing class in our index.php which is not available and not included anywhere.
<?php
include '../../KF/App.php';
$app = \KF\App::getInstance();
$app->run();
new Test();
In a normal scenario we get output an error that is more than logical:
Fatal error: Class 'Test' not found in ...
For now we’ve just described the mechanism but did not register it. That is why we don’t get our desired output from it. We need to register the method at least once (include it) and the most suitable place to do that is in the App class. Let’s see how this can be done.
<?php
namespace KF;
class App {
private static $_instance=null;
private function __construct() {
\KF\Loader::registerAutoLoad();
}
public function run(){
echo 'Devoted to Antagonism and Infamous';
}
/**
*
* [@return](/profile/return) \KF\App
*/
public static function getInstance(){
if (self::$_instance == null) {
self::$_instance = new \KF\App();
}
return self::$_instance;
}
}
Now the reason we include it in the App constructor is that this is the first thing that starts the application. Here goes the web service and will automatically call this private constructor. This time we get the same error that KFLoader is not loaded:
Fatal error: Class 'KF\Loader' not found in ...
PHP simply does not know where the loader is located so that is why we’ll manually include it in the App.php file:
<?php
namespace KF;
include_once 'Loader.php';
class App {
private static $_instance=null;
private function __construct() {
\KF\Loader::registerAutoLoad();
}
public function run(){
echo 'Devoted to Antagonism and Infamous';
}
/**
*
* [@return](/profile/return) \KF\App
*/
public static function getInstance(){
if (self::$_instance == null) {
self::$_instance = new \KF\App();
}
return self::$_instance;
}
}
This time we get an output of the method and the typical error for dislocation of a class because we did not realize a logic for non included classes. The idea is that as an answer to the function autoload, we got the name of the class that we are trying to make an instance of.
Test
Fatal error: Class 'Test' not found in...
Same goes with the usage of namespaces in the inclusion. We get the full path of the namespace and the name of the class that we’re trying to execute/parse.
Conclusion:
Been some time since I carried on with my series in this section. The book publishing and recent developments delayed that. This is supposed to be (actually will be for sure) a lot more extensive series of tutorials than my object-oriented ones. I intentionally omitted logical errors in the codes and then explained what was causing them in order for members to grasp the concept behind the MVC model. Next tutorial will be covering configs and front controllers of our framework. Thanks for reading!
what u need is what u get when u’re hacker !!! xx

- @IAmDevil
Its good to be back! :D
As always, Keepers tutorials are very dense and deep! For me to understand it, I have to use Google (wiki), not because it is unclear, but my knowledge of the subject is limited or nonexistent. Thank You.
I’d rather see folks doubt what’s true than accept what isn’t.
