ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
代码如下: ``` // 自动加载 public static function autoload($class) { // 检测命名空间别名 if (!empty(self::$namespaceAlias)) { $namespace = dirname($class); if (isset(self::$namespaceAlias[$namespace])) { $original = self::$namespaceAlias[$namespace] . '\\' . basename($class); if (class_exists($original)) { return class_alias($original, $class, false); } } } if ($file = self::findFile($class)) { // Win环境严格区分大小写 if (IS_WIN && pathinfo($file, PATHINFO_FILENAME) != pathinfo(realpath($file), PATHINFO_FILENAME)) { return false; } __include_file($file); return true; } } ``` # 1:检测命名空间别名(我们未定义别名)所以直接执行下面的文件查找 # 2:判断类所在的文件是否存在 调用自身类的findFile方法 findFile方法如下: ``` private static function findFile($class) { if (!empty(self::$map[$class])) { // 类库映射 return self::$map[$class]; } // 查找 PSR-4 $logicalPathPsr4 = strtr($class, '\\', DS) . EXT; $first = $class[0]; if (isset(self::$prefixLengthsPsr4[$first])) { foreach (self::$prefixLengthsPsr4[$first] as $prefix => $length) { if (0 === strpos($class, $prefix)) { foreach (self::$prefixDirsPsr4[$prefix] as $dir) { if (is_file($file = $dir . DS . substr($logicalPathPsr4, $length))) { return $file; } } } } } // 查找 PSR-4 fallback dirs foreach (self::$fallbackDirsPsr4 as $dir) { if (is_file($file = $dir . DS . $logicalPathPsr4)) { return $file; } } // 查找 PSR-0 if (false !== $pos = strrpos($class, '\\')) { // namespaced class name $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) . strtr(substr($logicalPathPsr4, $pos + 1), '_', DS); } else { // PEAR-like class name $logicalPathPsr0 = strtr($class, '_', DS) . EXT; } if (isset(self::$prefixesPsr0[$first])) { foreach (self::$prefixesPsr0[$first] as $prefix => $dirs) { if (0 === strpos($class, $prefix)) { foreach ($dirs as $dir) { if (is_file($file = $dir . DS . $logicalPathPsr0)) { return $file; } } } } } // 查找 PSR-0 fallback dirs foreach (self::$fallbackDirsPsr0 as $dir) { if (is_file($file = $dir . DS . $logicalPathPsr0)) { return $file; } } return self::$map[$class] = false; } ``` #### 1:先从类库映射查找(未查到) #### 2:查找 PSR-4 $logicalPathPsr4 = strtr($class, '\\', DS) . EXT; 函数strtr的用法:请到[strtr()](http://www.runoob.com/php/func-string-strtr.html) 我们输出打印$logicalPathPsr4得到结果如下: ~~~ string(15) "think\Route.php" string(16) "think\Config.php" string(18) "think\Validate.php" string(17) "think\Console.php" string(15) "think\Error.php" string(13) "think\App.php" string(17) "think\Request.php" string(14) "think\Hook.php" string(13) "think\Env.php" string(14) "think\Lang.php" string(13) "think\Log.php" string(30) "app\index\controller\Index.php" string(20) "think\Controller.php" string(14) "think\View.php" string(27) "think\view\driver\Think.php" string(18) "think\Template.php" string(30) "think\template\driver\File.php" string(18) "think\Response.php" string(19) "think\response\.php" string(17) "think\Session.php" string(25) "think\log\driver\File.php" ~~~  获取类所在的文件路径 $first = $class[0]; 输出打印 echo $first."<br/> "获得类文件路径的首字母 t t t t t t t t t t t a t t t t t t t t t 我们先打印出命名空间长度映射数组 var_dump(self::$prefixLengthsPsr4); ``` array(5) { ["t"]=> array(9) { ["think\worker\"]=> int(13) ["think\oracle\"]=> int(13) ["think\mongo\"]=> int(12) ["think\migration\"]=> int(16) ["think\helper\"]=> int(13) ["think\composer\"]=> int(15) ["think\captcha\"]=> int(14) ["think\"]=> int(6) ["traits\"]=> int(7) } ["a"]=> array(1) { ["app\"]=> int(4) } ["W"]=> array(1) { ["Workerman\"]=> int(10) } ["P"]=> array(1) { ["Phinx\"]=> int(6) } ["b"]=> array(1) { ["behavior\"]=> int(9) } } ``` 然后通过isset(self::$prefixLengthsPsr4[$first])判断$first(上面打印出来的t和a) 开头的数组是否存在 通过打印输出可知 显然存在 存在则对其进行遍历 var_dump(self::$prefixLengthsPsr4[$first]);//这里会根据加载的类名个数去输出,这里只输出众多加载类名里的第一个长度映射数组 ``` array(9) { ["think\worker\"]=> int(13) ["think\oracle\"]=> int(13) ["think\mongo\"]=> int(12) ["think\migration\"]=> int(16) ["think\helper\"]=> int(13) ["think\composer\"]=> int(15) ["think\captcha\"]=> int(14) ["think\"]=> int(6) ["traits\"]=> int(7) } ``` ``` foreach (self::$prefixLengthsPsr4[$first] as $prefix => $length) { if (0 === strpos($class, $prefix)) { /* var_dump($logicalPathPsr4); echo $prefix.'<br>';//通过打印可知这里便会将符合条件的类数据筛选出 string(15) "think\\Route.php" think\ string(16) "think\\Config.php" think\ string(18) "think\\Validate.php" think\ string(17) "think\\Console.php" think\ string(15) "think\\Error.php" think\ string(13) "think\\App.php" think\ string(17) "think\\Request.php" think\ string(14) "think\\Hook.php" think\ string(13) "think\\Env.php" think\ string(14) "think\\Lang.php" think\ string(13) "think\\Log.php" think\ string(30) "app\\index\\controller\\Index.php" app\ string(20) "think\\Controller.php" think\ string(14) "think\\View.php" think\ string(27) "think\\view\\driver\\Think.php" think\ string(18) "think\\Template.php" think\ string(30) "think\\template\\driver\\File.php" think\ string(18) "think\\Response.php" think\ string(19) "think\\response\\.php" think\ string(17) "think\\Session.php" think\ string(25) "think\\log\\driver\\File.php" think\ */ foreach (self::$prefixDirsPsr4[$prefix] as $dir) { if (is_file($file = $dir . DS . substr($logicalPathPsr4, $length))) { /*打印输出var_dump($file); string(67) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Route.php" string(68) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Config.php" string(70) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Validate.php" string(69) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Console.php" string(67) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Error.php" string(65) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\App.php" string(69) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Request.php" string(66) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Hook.php" string(65) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Env.php" string(66) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Lang.php" string(65) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Log.php" string(84) "C:\phpStudy\PHPTutorial\WWW\ceshi1\public/../application/\index\controller\Index.php" string(72) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Controller.php" string(66) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\View.php" string(79) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\view\driver\Think.php" string(70) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Template.php" string(82) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\template\driver\File.php" string(70) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Response.php" string(69) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\Session.php" string(77) "C:\phpStudy\PHPTutorial\WWW\ceshi1\thinkphp\library\think\log\driver\File.php"*/ return $file;//返回文件路径数据 } } } } ``` ``` //如果文件没找到则到extend扩展目录里面去寻找 // 查找 PSR-4 fallback dirs foreach (self::$fallbackDirsPsr4 as $dir) { if (is_file($file = $dir . DS . $logicalPathPsr4)) { return $file; } } ``` ``` // 如果未查到 则查 PSR-0 if (false !== $pos = strrpos($class, '\\')) { // namespaced class name $logicalPathPsr0 = substr($logicalPathPsr4, 0, $pos + 1) . strtr(substr($logicalPathPsr4, $pos + 1), '_', DS); } else { // PEAR-like class name $logicalPathPsr0 = strtr($class, '_', DS) . EXT; } if (isset(self::$prefixesPsr0[$first])) { foreach (self::$prefixesPsr0[$first] as $prefix => $dirs) { if (0 === strpos($class, $prefix)) { foreach ($dirs as $dir) { if (is_file($file = $dir . DS . $logicalPathPsr0)) { return $file; } } } } } // 查找 PSR-0 fallback dirs foreach (self::$fallbackDirsPsr0 as $dir) { if (is_file($file = $dir . DS . $logicalPathPsr0)) { return $file; } } return self::$map[$class] = false; } ``` ``` // 找不到则设置映射为 false 并返回 return self::$classMap\[$class\] = false; ```