I'm studying on spl_autoload_register(). I know what it is for ,just wondering how to use it practically. I searched for few references over the net for these, and each one has it's own style of approach. However I'm interested to understand the below script which found in one of my ebook.
WHat these three lines doing, I can understand roughly but not exactly. The autoload function receives a parameter $class, but why the below not dealing with it at all? For which class or item its defining the path?
1) get_include_path()
2) What is this doing? $flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$paths = explode(PATH_SEPARATOR, get_include_path());
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$file = strtolower(str_replace("\\", DIRECTORY_SEPARATOR, trim($class, "\\"))).".php";
Script
function autoload($class)
{
$paths = explode(PATH_SEPARATOR, get_include_path());
$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;
$file = strtolower(str_replace("\\", DIRECTORY_SEPARATOR, trim($class, "\\"))).".php";
foreach ($paths as $path)
{
$combined = $path.DIRECTORY_SEPARATOR.$file;
if (file_exists($combined))
{
include($combined);
return;
}
}
throw new Exception("{$class} not found");
}
class Autoloader
{
public static function autoload($class)
{
autoload($class);
}
}
spl_autoload_register('autoload');
spl_autoload_register(array('autoloader', 'autoload'));
// these can only be called within a class context…
// spl_autoload_register(array($this, 'autoload'));
// spl_autoload_register(__CLASS__.'::load');