duanli12176 2014-12-05 11:13
浏览 32
已采纳

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'));
  • 写回答

2条回答 默认 最新

  • dragon321723 2014-12-05 11:25
    关注

    In PHP namespaces are delimitered with a backslash as you can see in the documentation:

    $c = new \my ame\MyClass; // see "Global Space" section

    When you see the pattern uses two backslashes, this is because a backlash itself has a special meaning, namely as an escape character, as you again can see in the documentation.

    In particular, if you want to match a backslash, you write "\\".

    So in order to match the backslash you need to escape it, and end up with two backslashes.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突