I am breaking down a MVC framework created from a tutorial and I’m confused by the code below and the process. I don’t know why they created a class in a separate file and not just use the code in the first file. Here is the code from the first file named utility.php
function __autoload($class_name)
{
$inc = null;
require_once '../library/autoload.php';
$inc = new autoload($class_name);
}
Here is the code from the autoload.php
<?php
class autoload
{
public function __construct($class)
{
/*The next line is also in the utility.php file. Each occurance is to set up
*
*/
$paths = parse_ini_file('../application/config/paths.ini.php', true);
foreach($paths['include_paths'] as $path):
$class = str_replace('_', '/', $class);
$file = $path.$class.'.php';
if(file_exists($file)):
include $file;
return;
endif;
endforeach;
if(strstr($class, 'Controller')):
throw new gException('Class \\''.$class.'\\' could not be loaded by the autoloader');
endif;
}
}
Maybe I’m not understanding the what the code in the utility.php file is doing.
Thanks in advance for any help.
Thanks for replying.
logic_earth: Thanks for clarifying what I was thinking. The entire application seemed a bit heavy to me so as I’m breaking this down to learn MVC I’ll keep a look out for this type of practice.
hash: Thanks for the link. I’ll keep that in mind for future study. Right now I’m feeling a bit overwhelmed just breaking this code down. That is I have three tutorials that approach MVC framework in different ways and I’m breaking down each parallel to each other. My ultimate goal is to train my mind to think about programming from scratch. e.g. This is what I want the program to do and this is what I need from php to accomplish it.
Cheers!
Agree, you could use a class or a function, but both is a little silly.
You should also check out http://php.net/manual/en/function.spl-autoload-register.php
If you need a third party lib that wants to autoload as well, then __autoload is not much good.