Build your own MVC Framework - Step One: Getting Started, Page 2
Alright, my Pet Peeve. Every time you create a new class you have to add that file to your includes file, right? And no matter whether that file is actually doing anything or not, it is included, right?
Wrong. That ship has sailed.
We are going to use PHP's magic autoloader function to find the classes that we have instantiated and then we are going to include those files according to the class name. More on that in a minute. First, in our index.php file, let's build the autoloader function:
Figure 5: Code - index.php
__autoloadis a PHP function that grabs all class names that have been instantiated and passes them into itself. What we then do is use it to instantiate a new class and pass our classname into that. This may seem like a lot of work just to do that, but trust me--I have been around the block a hundred times with this. It works well. You will see the proof in due course.
Now we need to create the 'library/autoload.php' file that we included inside our
Figure 6: autoload.php added
Now, inside the autoload.php file we create the autoload class as follows:
Click here for larger image
Figure 7: Code - library/autoload.php
The first thing that happens here, on line 7, is that we define an array of include paths. These paths are setup inside a .ini file inside the application/configs directory, and we name the file paths.ini.php, as shown below:
Figure 8: paths.ini.php added
And inside paths.ini.php we put the code shown below:
Click here for larger image
Figure 9: Code - application/paths.ini.php
Now, what the function
parse_ini_file does is go to the url passed into the first argument of the function, and read the file. The second argument, boolean true or false, tells the parser whether you have separate section in square brackets
 or not. The function returns an array which we handle on line 9. On line 10, we are removing the underscores from the class name and replacing it with forward slashes. The reason for that is our class names will map out the location of the file containing the class. So a class named controller_abstract would either be found at ../application/controller/abstract.php or ../library/controller/abstract.php. Our autoloader will look in all places listed in our paths.ini.php file until it finds the file, and then will include it.
At this point we have our site architecture basically in place, we have an autoloader that includes instantiated classes and we have a config directory with (so far) one ini file defining the site paths in it.
Next week we explore the bootstrap file more deftly, as well as why I am throwing a thing called
gExceptionaround in my autoload class. We will also build the registry and the router, bringing us very close to having a workable MVC model as a foundation for our Framework.
Until next week,
Marc Steven Plotz
Marc Plotz is a Senior Web Developer for a major South African web development company and regularly writes articles on various topics for online web development article publishers. He specializes in the development of enterprise-class web applications and rapid application development frameworks. He lives in Pretoria, South Africa, with his wife and two children.