The index.php file is our single point of access. As such, it provides the ideal space for declaring variables and site wide configurations. It is in this file that we will also call a helper file to initialize a few values. This file will be called init.php and it will be placed in includes directory as shown in the directory structure.
The index.php file therefore, will begin like this:
<?php
error_reporting(E_ALL);
$site_path =realpath(dirname(__FILE__));
define('__SITE_PATH', $site_path);
include'include/init.php';
?>
The index.php file is so far only sets the error reporting, includes the init file, and defines the site path constant, with this file in place, and the .htaccess file we can begin to build the registry. In this MVC framework, the registry object is passed to other objects and contains site wide variables without the use globals. To create a new registry object, we use the init.php file in the includes directory.
To create new object we need to include the registry class definition file. During the building of the MVC framework we will be including the application class files directly. Any PHP class definition files which are used by the model will be autoloaded as they can become quite cumbersome with larger applications. To alleviate some of this PHP has to __autoload function to help us out. After the application includes, the __autoload function will immediately follow to load class definition files automatically when they are required by the system. That is, when the new keyword is used. The class definitions will be stored in a directory called model.
The includes/init.php file should look like this:
<?php
include__SITE_PATH . '/application/' . 'controller_base.class.php';
include__SITE_PATH . '/applicaiton/' . 'registry/class.php';
include__SITE_PATH . '/application/' . 'router.class.php';
include__SITE_PATH . 'applicaiton/' . 'template.class.php';
function__autoload($class_name) {
$filename = strtolower($class_name) . 'class.php';
$file = __SITE_PATH. '/model/' . $filename;
if (file_exists($file) == false)
{
return false;
}
include ($file);
}
$registry = new registry;
?>
Here is should be noted that the autoload function uses a naming convention for the class definition files to be included. They must all follow the convention of ending in .class.php and the class name must be that of the .class.php file name. So that to create a new "news" object the class definition file name must be news.class.php and the class must be named "news". With these files in place we are well on the way, however our MVC dies not do anything yet. In fact, if you tried to access the index.php file now, you would get many errors about missing files. Mostly from the files in the application directory.
Lets begin by creating those files each can be blank or simply contain
<?php
?>
The files to create in the application directory are:
Note that although these files are not autoloaded. We have still maintained the same naming convention by calling the files .class.php