php spl自动加载寄存器问题

These codes are copied from the book << Pro php mvc >>,I think they miss some lines,there are

$flags = PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE;

but no preg_split

and I can't figure out the line

$file = strtolower(str_replace("\\", DIRECTORY_SEPARATOR, trim($class, "\\"))).".php";

it trimed double back slashes first,then try to replace double back slashes with DIRECTORY_SEPARATOR ,how??

What circumstance it will have double back slashes in a class name?

the codes:

=------

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'));
查看全部
duanli12176
duanli12176
2014/12/05 11:13
  • php
  • 点赞
  • 收藏
  • 回答
    私信
满意答案
查看全部

2个回复