Error::register();
没有include Error.php走sql_autoload_register()中调用的autoload()
$file = self::findFile($class)
由注册自动加载(一)得属性:
//绝对路径改相对路径好理解
$prefixLengthsPsr4 = [
't' => array ('think\\composer\\' => 15,'think\\'=>6,'traits\\'=>7),
'a' =>array ('app\\' => 4,),
'P' =>array ('PHPMailer\\PHPMailer\\' => 20,),
];
$prefixDirsPsr4 = [
'think\\composer\\' => array (0 => __DIR__ . '/..' . '/topthink/think-installer/src',),
'app\\' =>array (0 => __DIR__ . '/../..' . '/application',),
'PHPMailer\\PHPMailer\\' => array (0 => __DIR__ . '/..' . '/phpmailer/phpmailer/src',),
'think\\' => array (0 => __DIR__ . '/../../thinkphp/think',),
'traits\\' => array (0 => __DIR__ . '/../../thinkphp/traits',),
];
$classMap = array (
'Composer\\InstalledVersions' => __DIR__ . '/..' . '/composer/InstalledVersions.php',
);
$fallbackDirsPsr4 = array(
'/../../extend'
);
findFile函数分析
if (!empty(self::$classMap[$class])) {
// 类库映射
return self::$classMap[$class];
}
$class = 'think\Error',由上面的属性可得知不存在这个类库映射,因此不走这条条件语句
$logicalPathPsr4 = strtr($class, '\\', DIRECTORY_SEPARATOR) . '.php';
$logicalPathPsr4 = 'think\\Error.php'
$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 . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length))) {
return $file;
}
}
}
}
}
$first = 't';$prefix='think\composer','think','traits';
$file = $dir . DIRECTORY_SEPARATOR . substr($logicalPathPsr4, $length)='/../../thinkphp/think'./.Error.php
__include_file($file);
function __include_file($file)
{
return include $file;
}
include 文件
由上可知,文件是否能够成功的引入取决于findFile方法
// 查找 PSR-4 fallback dirs
foreach (self::$fallbackDirsPsr4 as $dir) {
if (is_file($file = $dir . DIRECTORY_SEPARATOR . $logicalPathPsr4)) {
return $file;
}
}
$fallbackDirsPsr4中文件引入,extend中文件引入,$classMap中文件引入