yii2自动加载机制详解---Yii.php

唐景山
2023-12-01

一、spl_autoload_register()自动加载函数

官方函数地址:https://www.php.net/manual/en/function.spl-autoload-register.php

spl_autoload_register([callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]]):bool
用spl提供的__autoload队列注册一个函数。如果队列尚未激活,它将被激活。

(1)如果你的代码具有现有的__autoload()函数,则必须在__autoload队列中显式注册该函数。这是因为spl_autoload_register()将通过spl_autoload()或spl_autoload_call()有效地替换__autoload()函数的引擎缓存。详见例2
(2)如果必须有多个自动加载功能,则spl_autoload_register()允许这样做。它有效地创建了自动加载功能队列,并按照定义的顺序遍历每个功能。相反,__autoload()只能定义一次。
(3)之所以叫调用队列而不是调用栈,是因为把函数注册至spl的__autoload函数栈中并不是栈,而是队列。先注册的函数,优先遍历。

//----------例1----------//
//Demo.php文件位置:/Users/zhangsan/Demo.php
<?php
class Demo {
	public function test() {
		echo 'test';
	}
}

//调用文件位置:/Users/zhangsan/example1.php
<?php
function my_autoloader($class_name) {
	require_once ('/Users/zhangsan/'.$class_name.'.php');
}
spl_autoload_register('my_autoloader');
$demo = new Demo();
$demo->test();//test
//----------例2----------//
//Demo.php文件位置:/Users/zhangsan/Demo.php
<?php
class Demo {
	public function test() {
		echo 'test';
	}
}

//调用文件位置:/Users/zhangsan/example2.php
<?php
function __autoload($class_name) {
	require_once ('/Users/zhangsan/'.$class_name.'.php');
}
spl_autoload_register('__autoload');
$demo = new Demo();
$demo->test();//test
/*
Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in /Users/xiafen/web/test.xf.com/index32.php on line 17
test
*/

 

二、Yii2自动加载机制的实现

在Yii中,所有类、接口、Traits都可以使用类的自动加载机制实现在调用前自动加载。

项目入口文件 web/index.php

defined('YII_DEBUG') or define('YII_DEBUG', false);
defined('YII_ENV') or define('YII_ENV', 'prod');
// 这个是第三方的autoloader
require(__DIR__ . '/../../vendor/autoload.php');
// 这个是Yii的Autoloader,放在最后面,确保其插入的autoloader会放在最前面
require(__DIR__ . '/../../vendor/yiisoft/yii2/Yii.php');
// 后面不应再有autoloader了
require(__DIR__ . '/../../common/config/aliases.php');
$config = yii\helpers\ArrayHelper::merge(
    require(__DIR__ . '/../../common/config/main.php'),
    require(__DIR__ . '/../../common/config/main-local.php'),
    require(__DIR__ . '/../config/main.php'),
    require(__DIR__ . '/../config/main-local.php')
);
$application = new yii\web\Application($config);
$application->run();

解析:主要看点在于第三方autoloader与Yii 实现的autoloader的顺序。不管第三方的代码是如何使用 spl_autoload_register() 来注册自己的autoloader的,只要Yii 的代码在最后面,就可以确保其可以将自己的autoloader插入到整个autoloder 栈的最前面,从而在需要时最先被调用

vendor/yiisoft/yii2/Yii.php

require(__DIR__ . '/BaseYii.php');
class Yii extends \yii\BaseYii
{
}
// 重点看这个 spl_autoload_register
spl_autoload_register(['Yii', 'autoload'], true, true);
// 下面的语句读取了一个映射表
Yii::$classMap = include(__DIR__ . '/classes.php');
Yii::$container = new yii\di\Container;

vendor/yiisoft/yii2/BaseYii.php

    public static function autoload($className)
    {
        if (isset(static::$classMap[$className])) {
            $classFile = static::$classMap[$className];
            if ($classFile[0] === '@') {
                $classFile = static::getAlias($classFile);
            }
        } elseif (strpos($className, '\\') !== false) {
            $classFile = static::getAlias('@' . str_replace('\\', '/', $className) . '.php', false);
            if ($classFile === false || !is_file($classFile)) {
                return;
            }
        } else {
            return;
        }

        include $classFile;

        if (YII_DEBUG && !class_exists($className, false) && !interface_exists($className, false) && !trait_exists($className, false)) {
            throw new UnknownClassException("Unable to find '$className' in file: $classFile. Namespace missing?");
        }
    }

解析:
(1)将 classes.php 读取到 Yii::$classMap 中,保存了一个映射表。Yii所有的核心类都已经写入到这个 classes.php 文件中,所以,核心类的加载是最便捷,最快的

(2)调用了 spl_autoload_register(['Yii', 'autoload', true, true]) ,将 Yii::autoload() 作为autoloader插入到栈的最前面了。此处并未对 BaseYii::autoload() 进行重载,所以,这个 spl_autoload_register() 实际上将 BaseYii::autoload() 注册为autoloader。如果,你要实现自己的autoloader,可以在 Yii 类的代码中,对 autoload() 进行重载。

vendor/yiisoft/yii2/classes.php 

return [
  'yii\base\Action' => YII2_PATH . '/base/Action.php',
  'yii\base\ActionEvent' => YII2_PATH . '/base/ActionEvent.php',
  'yii\base\ActionFilter' => YII2_PATH . '/base/ActionFilter.php',
  'yii\base\Application' => YII2_PATH . '/base/Application.php',
  'yii\base\ArrayAccessTrait' => YII2_PATH . '/base/ArrayAccessTrait.php',
  'yii\base\Arrayable' => YII2_PATH . '/base/Arrayable.php',
  'yii\base\ArrayableTrait' => YII2_PATH . '/base/ArrayableTrait.php',
  'yii\base\BaseObject' => YII2_PATH . '/base/BaseObject.php',
  'yii\base\Behavior' => YII2_PATH . '/base/Behavior.php',
  'yii\base\BootstrapInterface' => YII2_PATH . '/base/BootstrapInterface.php',
  'yii\base\Component' => YII2_PATH . '/base/Component.php',
  'yii\base\Configurable' => YII2_PATH . '/base/Configurable.php',
  'yii\base\Controller' => YII2_PATH . '/base/Controller.php',
  'yii\base\DynamicContentAwareInterface' => YII2_PATH . '/base/DynamicContentAwareInterface.php',
  'yii\base\DynamicContentAwareTrait' => YII2_PATH . '/base/DynamicContentAwareTrait.php',
  'yii\base\DynamicModel' => YII2_PATH . '/base/DynamicModel.php',
  'yii\base\ErrorException' => YII2_PATH . '/base/ErrorException.php',
  'yii\base\ErrorHandler' => YII2_PATH . '/base/ErrorHandler.php',
  'yii\base\Event' => YII2_PATH . '/base/Event.php',
  'yii\base\Exception' => YII2_PATH . '/base/Exception.php',
  'yii\base\ExitException' => YII2_PATH . '/base/ExitException.php',
  'yii\base\InlineAction' => YII2_PATH . '/base/InlineAction.php',
  'yii\base\InvalidArgumentException' => YII2_PATH . '/base/InvalidArgumentException.php',
  'yii\base\InvalidCallException' => YII2_PATH . '/base/InvalidCallException.php',
  'yii\base\InvalidConfigException' => YII2_PATH . '/base/InvalidConfigException.php',
  'yii\base\InvalidParamException' => YII2_PATH . '/base/InvalidParamException.php',
  'yii\base\InvalidRouteException' => YII2_PATH . '/base/InvalidRouteException.php',
  'yii\base\InvalidValueException' => YII2_PATH . '/base/InvalidValueException.php',
  'yii\base\Model' => YII2_PATH . '/base/Model.php',
  'yii\base\ModelEvent' => YII2_PATH . '/base/ModelEvent.php',
  'yii\base\Module' => YII2_PATH . '/base/Module.php',
  'yii\base\NotSupportedException' => YII2_PATH . '/base/NotSupportedException.php',
  'yii\base\Object' => YII2_PATH . '/base/Object.php',
  'yii\base\Request' => YII2_PATH . '/base/Request.php',
  'yii\base\Response' => YII2_PATH . '/base/Response.php',
  'yii\base\Security' => YII2_PATH . '/base/Security.php',
  'yii\base\StaticInstanceInterface' => YII2_PATH . '/base/StaticInstanceInterface.php',
  'yii\base\StaticInstanceTrait' => YII2_PATH . '/base/StaticInstanceTrait.php',
  'yii\base\Theme' => YII2_PATH . '/base/Theme.php',
  'yii\base\UnknownClassException' => YII2_PATH . '/base/UnknownClassException.php',
  'yii\base\UnknownMethodException' => YII2_PATH . '/base/UnknownMethodException.php',
  'yii\base\UnknownPropertyException' => YII2_PATH . '/base/UnknownPropertyException.php',
  'yii\base\UserException' => YII2_PATH . '/base/UserException.php',
  'yii\base\View' => YII2_PATH . '/base/View.php',
  'yii\base\ViewContextInterface' => YII2_PATH . '/base/ViewContextInterface.php',
  'yii\base\ViewEvent' => YII2_PATH . '/base/ViewEvent.php',
  'yii\base\ViewNotFoundException' => YII2_PATH . '/base/ViewNotFoundException.php',
  'yii\base\ViewRenderer' => YII2_PATH . '/base/ViewRenderer.php',
  'yii\base\Widget' => YII2_PATH . '/base/Widget.php',
  'yii\base\WidgetEvent' => YII2_PATH . '/base/WidgetEvent.php',
  'yii\behaviors\AttributeBehavior' => YII2_PATH . '/behaviors/AttributeBehavior.php',
  'yii\behaviors\AttributeTypecastBehavior' => YII2_PATH . '/behaviors/AttributeTypecastBehavior.php',
  'yii\behaviors\AttributesBehavior' => YII2_PATH . '/behaviors/AttributesBehavior.php',
  'yii\behaviors\BlameableBehavior' => YII2_PATH . '/behaviors/BlameableBehavior.php',
  'yii\behaviors\CacheableWidgetBehavior' => YII2_PATH . '/behaviors/CacheableWidgetBehavior.php',
  'yii\behaviors\SluggableBehavior' => YII2_PATH . '/behaviors/SluggableBehavior.php',
  'yii\behaviors\TimestampBehavior' => YII2_PATH . '/behaviors/TimestampBehavior.php',
  'yii\caching\ApcCache' => YII2_PATH . '/caching/ApcCache.php',
  'yii\caching\ArrayCache' => YII2_PATH . '/caching/ArrayCache.php',
  'yii\caching\Cache' => YII2_PATH . '/caching/Cache.php',
  'yii\caching\CacheInterface' => YII2_PATH . '/caching/CacheInterface.php',
  'yii\caching\ChainedDependency' => YII2_PATH . '/caching/ChainedDependency.php',
  'yii\caching\DbCache' => YII2_PATH . '/caching/DbCache.php',
  'yii\caching\DbDependency' => YII2_PATH . '/caching/DbDependency.php',
  'yii\caching\DbQueryDependency' => YII2_PATH . '/caching/DbQueryDependency.php',
  'yii\caching\Dependency' => YII2_PATH . '/caching/Dependency.php',
  'yii\caching\DummyCache' => YII2_PATH . '/caching/DummyCache.php',
  'yii\caching\ExpressionDependency' => YII2_PATH . '/caching/ExpressionDependency.php',
  'yii\caching\FileCache' => YII2_PATH . '/caching/FileCache.php',
  'yii\caching\FileDependency' => YII2_PATH . '/caching/FileDependency.php',
  'yii\caching\MemCache' => YII2_PATH . '/caching/MemCache.php',
  'yii\caching\MemCacheServer' => YII2_PATH . '/caching/MemCacheServer.php',
  'yii\caching\TagDependency' => YII2_PATH . '/caching/TagDependency.php',
  'yii\caching\WinCache' => YII2_PATH . '/caching/WinCache.php',
  'yii\caching\XCache' => YII2_PATH . '/caching/XCache.php',
  'yii\caching\ZendDataCache' => YII2_PATH . '/caching/ZendDataCache.php',
  'yii\captcha\Captcha' => YII2_PATH . '/captcha/Captcha.php',
  'yii\captcha\CaptchaAction' => YII2_PATH . '/captcha/CaptchaAction.php',
  'yii\captcha\CaptchaAsset' => YII2_PATH . '/captcha/CaptchaAsset.php',
  'yii\captcha\CaptchaValidator' => YII2_PATH . '/captcha/CaptchaValidator.php',
  'yii\data\ActiveDataFilter' => YII2_PATH . '/data/ActiveDataFilter.php',
  'yii\data\ActiveDataProvider' => YII2_PATH . '/data/ActiveDataProvider.php',
  'yii\data\ArrayDataProvider' => YII2_PATH . '/data/ArrayDataProvider.php',
  'yii\data\BaseDataProvider' => YII2_PATH . '/data/BaseDataProvider.php',
  'yii\data\DataFilter' => YII2_PATH . '/data/DataFilter.php',
  'yii\data\DataProviderInterface' => YII2_PATH . '/data/DataProviderInterface.php',
  'yii\data\Pagination' => YII2_PATH . '/data/Pagination.php',
  'yii\data\Sort' => YII2_PATH . '/data/Sort.php',
  'yii\data\SqlDataProvider' => YII2_PATH . '/data/SqlDataProvider.php',
  'yii\db\ActiveQuery' => YII2_PATH . '/db/ActiveQuery.php',
  'yii\db\ActiveQueryInterface' => YII2_PATH . '/db/ActiveQueryInterface.php',
  'yii\db\ActiveQueryTrait' => YII2_PATH . '/db/ActiveQueryTrait.php',
  'yii\db\ActiveRecord' => YII2_PATH . '/db/ActiveRecord.php',
  'yii\db\ActiveRecordInterface' => YII2_PATH . '/db/ActiveRecordInterface.php',
  'yii\db\ActiveRelationTrait' => YII2_PATH . '/db/ActiveRelationTrait.php',
  'yii\db\AfterSaveEvent' => YII2_PATH . '/db/AfterSaveEvent.php',
  'yii\db\ArrayExpression' => YII2_PATH . '/db/ArrayExpression.php',
  'yii\db\BaseActiveRecord' => YII2_PATH . '/db/BaseActiveRecord.php',
  'yii\db\BatchQueryResult' => YII2_PATH . '/db/BatchQueryResult.php',
  'yii\db\CheckConstraint' => YII2_PATH . '/db/CheckConstraint.php',
  'yii\db\ColumnSchema' => YII2_PATH . '/db/ColumnSchema.php',
  'yii\db\ColumnSchemaBuilder' => YII2_PATH . '/db/ColumnSchemaBuilder.php',
  'yii\db\Command' => YII2_PATH . '/db/Command.php',
  'yii\db\Connection' => YII2_PATH . '/db/Connection.php',
  'yii\db\Constraint' => YII2_PATH . '/db/Constraint.php',
  'yii\db\ConstraintFinderInterface' => YII2_PATH . '/db/ConstraintFinderInterface.php',
  'yii\db\ConstraintFinderTrait' => YII2_PATH . '/db/ConstraintFinderTrait.php',
  'yii\db\DataReader' => YII2_PATH . '/db/DataReader.php',
  'yii\db\DefaultValueConstraint' => YII2_PATH . '/db/DefaultValueConstraint.php',
  'yii\db\Exception' => YII2_PATH . '/db/Exception.php',
  'yii\db\Expression' => YII2_PATH . '/db/Expression.php',
  'yii\db\ExpressionBuilder' => YII2_PATH . '/db/ExpressionBuilder.php',
  'yii\db\ExpressionBuilderInterface' => YII2_PATH . '/db/ExpressionBuilderInterface.php',
  'yii\db\ExpressionBuilderTrait' => YII2_PATH . '/db/ExpressionBuilderTrait.php',
  'yii\db\ExpressionInterface' => YII2_PATH . '/db/ExpressionInterface.php',
  'yii\db\ForeignKeyConstraint' => YII2_PATH . '/db/ForeignKeyConstraint.php',
  'yii\db\IndexConstraint' => YII2_PATH . '/db/IndexConstraint.php',
  'yii\db\IntegrityException' => YII2_PATH . '/db/IntegrityException.php',
  'yii\db\JsonExpression' => YII2_PATH . '/db/JsonExpression.php',
  'yii\db\Migration' => YII2_PATH . '/db/Migration.php',
  'yii\db\MigrationInterface' => YII2_PATH . '/db/MigrationInterface.php',
  'yii\db\PdoValue' => YII2_PATH . '/db/PdoValue.php',
  'yii\db\PdoValueBuilder' => YII2_PATH . '/db/PdoValueBuilder.php',
  'yii\db\Query' => YII2_PATH . '/db/Query.php',
  'yii\db\QueryBuilder' => YII2_PATH . '/db/QueryBuilder.php',
  'yii\db\QueryExpressionBuilder' => YII2_PATH . '/db/QueryExpressionBuilder.php',
  'yii\db\QueryInterface' => YII2_PATH . '/db/QueryInterface.php',
  'yii\db\QueryTrait' => YII2_PATH . '/db/QueryTrait.php',
  'yii\db\Schema' => YII2_PATH . '/db/Schema.php',
  'yii\db\SchemaBuilderTrait' => YII2_PATH . '/db/SchemaBuilderTrait.php',
  'yii\db\SqlToken' => YII2_PATH . '/db/SqlToken.php',
  'yii\db\SqlTokenizer' => YII2_PATH . '/db/SqlTokenizer.php',
  'yii\db\StaleObjectException' => YII2_PATH . '/db/StaleObjectException.php',
  'yii\db\TableSchema' => YII2_PATH . '/db/TableSchema.php',
  'yii\db\Transaction' => YII2_PATH . '/db/Transaction.php',
  'yii\db\ViewFinderTrait' => YII2_PATH . '/db/ViewFinderTrait.php',
  'yii\db\conditions\AndCondition' => YII2_PATH . '/db/conditions/AndCondition.php',
  'yii\db\conditions\BetweenColumnsCondition' => YII2_PATH . '/db/conditions/BetweenColumnsCondition.php',
  'yii\db\conditions\BetweenColumnsConditionBuilder' => YII2_PATH . '/db/conditions/BetweenColumnsConditionBuilder.php',
  'yii\db\conditions\BetweenCondition' => YII2_PATH . '/db/conditions/BetweenCondition.php',
  'yii\db\conditions\BetweenConditionBuilder' => YII2_PATH . '/db/conditions/BetweenConditionBuilder.php',
  'yii\db\conditions\ConditionInterface' => YII2_PATH . '/db/conditions/ConditionInterface.php',
  'yii\db\conditions\ConjunctionCondition' => YII2_PATH . '/db/conditions/ConjunctionCondition.php',
  'yii\db\conditions\ConjunctionConditionBuilder' => YII2_PATH . '/db/conditions/ConjunctionConditionBuilder.php',
  'yii\db\conditions\ExistsCondition' => YII2_PATH . '/db/conditions/ExistsCondition.php',
  'yii\db\conditions\ExistsConditionBuilder' => YII2_PATH . '/db/conditions/ExistsConditionBuilder.php',
  'yii\db\conditions\HashCondition' => YII2_PATH . '/db/conditions/HashCondition.php',
  'yii\db\conditions\HashConditionBuilder' => YII2_PATH . '/db/conditions/HashConditionBuilder.php',
  'yii\db\conditions\InCondition' => YII2_PATH . '/db/conditions/InCondition.php',
  'yii\db\conditions\InConditionBuilder' => YII2_PATH . '/db/conditions/InConditionBuilder.php',
  'yii\db\conditions\LikeCondition' => YII2_PATH . '/db/conditions/LikeCondition.php',
  'yii\db\conditions\LikeConditionBuilder' => YII2_PATH . '/db/conditions/LikeConditionBuilder.php',
  'yii\db\conditions\NotCondition' => YII2_PATH . '/db/conditions/NotCondition.php',
  'yii\db\conditions\NotConditionBuilder' => YII2_PATH . '/db/conditions/NotConditionBuilder.php',
  'yii\db\conditions\OrCondition' => YII2_PATH . '/db/conditions/OrCondition.php',
  'yii\db\conditions\SimpleCondition' => YII2_PATH . '/db/conditions/SimpleCondition.php',
  'yii\db\conditions\SimpleConditionBuilder' => YII2_PATH . '/db/conditions/SimpleConditionBuilder.php',
  'yii\db\cubrid\ColumnSchemaBuilder' => YII2_PATH . '/db/cubrid/ColumnSchemaBuilder.php',
  'yii\db\cubrid\QueryBuilder' => YII2_PATH . '/db/cubrid/QueryBuilder.php',
  'yii\db\cubrid\Schema' => YII2_PATH . '/db/cubrid/Schema.php',
  'yii\db\cubrid\conditions\LikeConditionBuilder' => YII2_PATH . '/db/cubrid/conditions/LikeConditionBuilder.php',
  'yii\db\mssql\PDO' => YII2_PATH . '/db/mssql/PDO.php',
  'yii\db\mssql\QueryBuilder' => YII2_PATH . '/db/mssql/QueryBuilder.php',
  'yii\db\mssql\Schema' => YII2_PATH . '/db/mssql/Schema.php',
  'yii\db\mssql\SqlsrvPDO' => YII2_PATH . '/db/mssql/SqlsrvPDO.php',
  'yii\db\mssql\TableSchema' => YII2_PATH . '/db/mssql/TableSchema.php',
  'yii\db\mssql\conditions\InConditionBuilder' => YII2_PATH . '/db/mssql/conditions/InConditionBuilder.php',
  'yii\db\mssql\conditions\LikeConditionBuilder' => YII2_PATH . '/db/mssql/conditions/LikeConditionBuilder.php',
  'yii\db\mysql\ColumnSchema' => YII2_PATH . '/db/mysql/ColumnSchema.php',
  'yii\db\mysql\ColumnSchemaBuilder' => YII2_PATH . '/db/mysql/ColumnSchemaBuilder.php',
  'yii\db\mysql\JsonExpressionBuilder' => YII2_PATH . '/db/mysql/JsonExpressionBuilder.php',
  'yii\db\mysql\QueryBuilder' => YII2_PATH . '/db/mysql/QueryBuilder.php',
  'yii\db\mysql\Schema' => YII2_PATH . '/db/mysql/Schema.php',
  'yii\db\oci\ColumnSchemaBuilder' => YII2_PATH . '/db/oci/ColumnSchemaBuilder.php',
  'yii\db\oci\QueryBuilder' => YII2_PATH . '/db/oci/QueryBuilder.php',
  'yii\db\oci\Schema' => YII2_PATH . '/db/oci/Schema.php',
  'yii\db\oci\conditions\InConditionBuilder' => YII2_PATH . '/db/oci/conditions/InConditionBuilder.php',
  'yii\db\oci\conditions\LikeConditionBuilder' => YII2_PATH . '/db/oci/conditions/LikeConditionBuilder.php',
  'yii\db\pgsql\ArrayExpressionBuilder' => YII2_PATH . '/db/pgsql/ArrayExpressionBuilder.php',
  'yii\db\pgsql\ArrayParser' => YII2_PATH . '/db/pgsql/ArrayParser.php',
  'yii\db\pgsql\ColumnSchema' => YII2_PATH . '/db/pgsql/ColumnSchema.php',
  'yii\db\pgsql\JsonExpressionBuilder' => YII2_PATH . '/db/pgsql/JsonExpressionBuilder.php',
  'yii\db\pgsql\QueryBuilder' => YII2_PATH . '/db/pgsql/QueryBuilder.php',
  'yii\db\pgsql\Schema' => YII2_PATH . '/db/pgsql/Schema.php',
  'yii\db\sqlite\ColumnSchemaBuilder' => YII2_PATH . '/db/sqlite/ColumnSchemaBuilder.php',
  'yii\db\sqlite\Command' => YII2_PATH . '/db/sqlite/Command.php',
  'yii\db\sqlite\QueryBuilder' => YII2_PATH . '/db/sqlite/QueryBuilder.php',
  'yii\db\sqlite\Schema' => YII2_PATH . '/db/sqlite/Schema.php',
  'yii\db\sqlite\SqlTokenizer' => YII2_PATH . '/db/sqlite/SqlTokenizer.php',
  'yii\db\sqlite\conditions\InConditionBuilder' => YII2_PATH . '/db/sqlite/conditions/InConditionBuilder.php',
  'yii\db\sqlite\conditions\LikeConditionBuilder' => YII2_PATH . '/db/sqlite/conditions/LikeConditionBuilder.php',
  'yii\di\Container' => YII2_PATH . '/di/Container.php',
  'yii\di\Instance' => YII2_PATH . '/di/Instance.php',
  'yii\di\NotInstantiableException' => YII2_PATH . '/di/NotInstantiableException.php',
  'yii\di\ServiceLocator' => YII2_PATH . '/di/ServiceLocator.php',
  'yii\filters\AccessControl' => YII2_PATH . '/filters/AccessControl.php',
  'yii\filters\AccessRule' => YII2_PATH . '/filters/AccessRule.php',
  'yii\filters\AjaxFilter' => YII2_PATH . '/filters/AjaxFilter.php',
  'yii\filters\ContentNegotiator' => YII2_PATH . '/filters/ContentNegotiator.php',
  'yii\filters\Cors' => YII2_PATH . '/filters/Cors.php',
  'yii\filters\HostControl' => YII2_PATH . '/filters/HostControl.php',
  'yii\filters\HttpCache' => YII2_PATH . '/filters/HttpCache.php',
  'yii\filters\PageCache' => YII2_PATH . '/filters/PageCache.php',
  'yii\filters\RateLimitInterface' => YII2_PATH . '/filters/RateLimitInterface.php',
  'yii\filters\RateLimiter' => YII2_PATH . '/filters/RateLimiter.php',
  'yii\filters\VerbFilter' => YII2_PATH . '/filters/VerbFilter.php',
  'yii\filters\auth\AuthInterface' => YII2_PATH . '/filters/auth/AuthInterface.php',
  'yii\filters\auth\AuthMethod' => YII2_PATH . '/filters/auth/AuthMethod.php',
  'yii\filters\auth\CompositeAuth' => YII2_PATH . '/filters/auth/CompositeAuth.php',
  'yii\filters\auth\HttpBasicAuth' => YII2_PATH . '/filters/auth/HttpBasicAuth.php',
  'yii\filters\auth\HttpBearerAuth' => YII2_PATH . '/filters/auth/HttpBearerAuth.php',
  'yii\filters\auth\HttpHeaderAuth' => YII2_PATH . '/filters/auth/HttpHeaderAuth.php',
  'yii\filters\auth\QueryParamAuth' => YII2_PATH . '/filters/auth/QueryParamAuth.php',
  'yii\grid\ActionColumn' => YII2_PATH . '/grid/ActionColumn.php',
  'yii\grid\CheckboxColumn' => YII2_PATH . '/grid/CheckboxColumn.php',
  'yii\grid\Column' => YII2_PATH . '/grid/Column.php',
  'yii\grid\DataColumn' => YII2_PATH . '/grid/DataColumn.php',
  'yii\grid\GridView' => YII2_PATH . '/grid/GridView.php',
  'yii\grid\GridViewAsset' => YII2_PATH . '/grid/GridViewAsset.php',
  'yii\grid\RadioButtonColumn' => YII2_PATH . '/grid/RadioButtonColumn.php',
  'yii\grid\SerialColumn' => YII2_PATH . '/grid/SerialColumn.php',
  'yii\helpers\ArrayHelper' => YII2_PATH . '/helpers/ArrayHelper.php',
  'yii\helpers\BaseArrayHelper' => YII2_PATH . '/helpers/BaseArrayHelper.php',
  'yii\helpers\BaseConsole' => YII2_PATH . '/helpers/BaseConsole.php',
  'yii\helpers\BaseFileHelper' => YII2_PATH . '/helpers/BaseFileHelper.php',
  'yii\helpers\BaseFormatConverter' => YII2_PATH . '/helpers/BaseFormatConverter.php',
  'yii\helpers\BaseHtml' => YII2_PATH . '/helpers/BaseHtml.php',
  'yii\helpers\BaseHtmlPurifier' => YII2_PATH . '/helpers/BaseHtmlPurifier.php',
  'yii\helpers\BaseInflector' => YII2_PATH . '/helpers/BaseInflector.php',
  'yii\helpers\BaseIpHelper' => YII2_PATH . '/helpers/BaseIpHelper.php',
  'yii\helpers\BaseJson' => YII2_PATH . '/helpers/BaseJson.php',
  'yii\helpers\BaseMarkdown' => YII2_PATH . '/helpers/BaseMarkdown.php',
  'yii\helpers\BaseStringHelper' => YII2_PATH . '/helpers/BaseStringHelper.php',
  'yii\helpers\BaseUrl' => YII2_PATH . '/helpers/BaseUrl.php',
  'yii\helpers\BaseVarDumper' => YII2_PATH . '/helpers/BaseVarDumper.php',
  'yii\helpers\Console' => YII2_PATH . '/helpers/Console.php',
  'yii\helpers\FileHelper' => YII2_PATH . '/helpers/FileHelper.php',
  'yii\helpers\FormatConverter' => YII2_PATH . '/helpers/FormatConverter.php',
  'yii\helpers\Html' => YII2_PATH . '/helpers/Html.php',
  'yii\helpers\HtmlPurifier' => YII2_PATH . '/helpers/HtmlPurifier.php',
  'yii\helpers\Inflector' => YII2_PATH . '/helpers/Inflector.php',
  'yii\helpers\IpHelper' => YII2_PATH . '/helpers/IpHelper.php',
  'yii\helpers\Json' => YII2_PATH . '/helpers/Json.php',
  'yii\helpers\Markdown' => YII2_PATH . '/helpers/Markdown.php',
  'yii\helpers\ReplaceArrayValue' => YII2_PATH . '/helpers/ReplaceArrayValue.php',
  'yii\helpers\StringHelper' => YII2_PATH . '/helpers/StringHelper.php',
  'yii\helpers\UnsetArrayValue' => YII2_PATH . '/helpers/UnsetArrayValue.php',
  'yii\helpers\Url' => YII2_PATH . '/helpers/Url.php',
  'yii\helpers\VarDumper' => YII2_PATH . '/helpers/VarDumper.php',
  'yii\i18n\DbMessageSource' => YII2_PATH . '/i18n/DbMessageSource.php',
  'yii\i18n\Formatter' => YII2_PATH . '/i18n/Formatter.php',
  'yii\i18n\GettextFile' => YII2_PATH . '/i18n/GettextFile.php',
  'yii\i18n\GettextMessageSource' => YII2_PATH . '/i18n/GettextMessageSource.php',
  'yii\i18n\GettextMoFile' => YII2_PATH . '/i18n/GettextMoFile.php',
  'yii\i18n\GettextPoFile' => YII2_PATH . '/i18n/GettextPoFile.php',
  'yii\i18n\I18N' => YII2_PATH . '/i18n/I18N.php',
  'yii\i18n\Locale' => YII2_PATH . '/i18n/Locale.php',
  'yii\i18n\MessageFormatter' => YII2_PATH . '/i18n/MessageFormatter.php',
  'yii\i18n\MessageSource' => YII2_PATH . '/i18n/MessageSource.php',
  'yii\i18n\MissingTranslationEvent' => YII2_PATH . '/i18n/MissingTranslationEvent.php',
  'yii\i18n\PhpMessageSource' => YII2_PATH . '/i18n/PhpMessageSource.php',
  'yii\log\DbTarget' => YII2_PATH . '/log/DbTarget.php',
  'yii\log\Dispatcher' => YII2_PATH . '/log/Dispatcher.php',
  'yii\log\EmailTarget' => YII2_PATH . '/log/EmailTarget.php',
  'yii\log\FileTarget' => YII2_PATH . '/log/FileTarget.php',
  'yii\log\LogRuntimeException' => YII2_PATH . '/log/LogRuntimeException.php',
  'yii\log\Logger' => YII2_PATH . '/log/Logger.php',
  'yii\log\SyslogTarget' => YII2_PATH . '/log/SyslogTarget.php',
  'yii\log\Target' => YII2_PATH . '/log/Target.php',
  'yii\mail\BaseMailer' => YII2_PATH . '/mail/BaseMailer.php',
  'yii\mail\BaseMessage' => YII2_PATH . '/mail/BaseMessage.php',
  'yii\mail\MailEvent' => YII2_PATH . '/mail/MailEvent.php',
  'yii\mail\MailerInterface' => YII2_PATH . '/mail/MailerInterface.php',
  'yii\mail\MessageInterface' => YII2_PATH . '/mail/MessageInterface.php',
  'yii\mutex\DbMutex' => YII2_PATH . '/mutex/DbMutex.php',
  'yii\mutex\FileMutex' => YII2_PATH . '/mutex/FileMutex.php',
  'yii\mutex\Mutex' => YII2_PATH . '/mutex/Mutex.php',
  'yii\mutex\MysqlMutex' => YII2_PATH . '/mutex/MysqlMutex.php',
  'yii\mutex\OracleMutex' => YII2_PATH . '/mutex/OracleMutex.php',
  'yii\mutex\PgsqlMutex' => YII2_PATH . '/mutex/PgsqlMutex.php',
  'yii\rbac\Assignment' => YII2_PATH . '/rbac/Assignment.php',
  'yii\rbac\BaseManager' => YII2_PATH . '/rbac/BaseManager.php',
  'yii\rbac\CheckAccessInterface' => YII2_PATH . '/rbac/CheckAccessInterface.php',
  'yii\rbac\DbManager' => YII2_PATH . '/rbac/DbManager.php',
  'yii\rbac\Item' => YII2_PATH . '/rbac/Item.php',
  'yii\rbac\ManagerInterface' => YII2_PATH . '/rbac/ManagerInterface.php',
  'yii\rbac\Permission' => YII2_PATH . '/rbac/Permission.php',
  'yii\rbac\PhpManager' => YII2_PATH . '/rbac/PhpManager.php',
  'yii\rbac\Role' => YII2_PATH . '/rbac/Role.php',
  'yii\rbac\Rule' => YII2_PATH . '/rbac/Rule.php',
  'yii\rest\Action' => YII2_PATH . '/rest/Action.php',
  'yii\rest\ActiveController' => YII2_PATH . '/rest/ActiveController.php',
  'yii\rest\Controller' => YII2_PATH . '/rest/Controller.php',
  'yii\rest\CreateAction' => YII2_PATH . '/rest/CreateAction.php',
  'yii\rest\DeleteAction' => YII2_PATH . '/rest/DeleteAction.php',
  'yii\rest\IndexAction' => YII2_PATH . '/rest/IndexAction.php',
  'yii\rest\OptionsAction' => YII2_PATH . '/rest/OptionsAction.php',
  'yii\rest\Serializer' => YII2_PATH . '/rest/Serializer.php',
  'yii\rest\UpdateAction' => YII2_PATH . '/rest/UpdateAction.php',
  'yii\rest\UrlRule' => YII2_PATH . '/rest/UrlRule.php',
  'yii\rest\ViewAction' => YII2_PATH . '/rest/ViewAction.php',
  'yii\test\ActiveFixture' => YII2_PATH . '/test/ActiveFixture.php',
  'yii\test\ArrayFixture' => YII2_PATH . '/test/ArrayFixture.php',
  'yii\test\BaseActiveFixture' => YII2_PATH . '/test/BaseActiveFixture.php',
  'yii\test\DbFixture' => YII2_PATH . '/test/DbFixture.php',
  'yii\test\FileFixtureTrait' => YII2_PATH . '/test/FileFixtureTrait.php',
  'yii\test\Fixture' => YII2_PATH . '/test/Fixture.php',
  'yii\test\FixtureTrait' => YII2_PATH . '/test/FixtureTrait.php',
  'yii\test\InitDbFixture' => YII2_PATH . '/test/InitDbFixture.php',
  'yii\validators\BooleanValidator' => YII2_PATH . '/validators/BooleanValidator.php',
  'yii\validators\CompareValidator' => YII2_PATH . '/validators/CompareValidator.php',
  'yii\validators\DateValidator' => YII2_PATH . '/validators/DateValidator.php',
  'yii\validators\DefaultValueValidator' => YII2_PATH . '/validators/DefaultValueValidator.php',
  'yii\validators\EachValidator' => YII2_PATH . '/validators/EachValidator.php',
  'yii\validators\EmailValidator' => YII2_PATH . '/validators/EmailValidator.php',
  'yii\validators\ExistValidator' => YII2_PATH . '/validators/ExistValidator.php',
  'yii\validators\FileValidator' => YII2_PATH . '/validators/FileValidator.php',
  'yii\validators\FilterValidator' => YII2_PATH . '/validators/FilterValidator.php',
  'yii\validators\ImageValidator' => YII2_PATH . '/validators/ImageValidator.php',
  'yii\validators\InlineValidator' => YII2_PATH . '/validators/InlineValidator.php',
  'yii\validators\IpValidator' => YII2_PATH . '/validators/IpValidator.php',
  'yii\validators\NumberValidator' => YII2_PATH . '/validators/NumberValidator.php',
  'yii\validators\PunycodeAsset' => YII2_PATH . '/validators/PunycodeAsset.php',
  'yii\validators\RangeValidator' => YII2_PATH . '/validators/RangeValidator.php',
  'yii\validators\RegularExpressionValidator' => YII2_PATH . '/validators/RegularExpressionValidator.php',
  'yii\validators\RequiredValidator' => YII2_PATH . '/validators/RequiredValidator.php',
  'yii\validators\SafeValidator' => YII2_PATH . '/validators/SafeValidator.php',
  'yii\validators\StringValidator' => YII2_PATH . '/validators/StringValidator.php',
  'yii\validators\UniqueValidator' => YII2_PATH . '/validators/UniqueValidator.php',
  'yii\validators\UrlValidator' => YII2_PATH . '/validators/UrlValidator.php',
  'yii\validators\ValidationAsset' => YII2_PATH . '/validators/ValidationAsset.php',
  'yii\validators\Validator' => YII2_PATH . '/validators/Validator.php',
  'yii\web\Application' => YII2_PATH . '/web/Application.php',
  'yii\web\AssetBundle' => YII2_PATH . '/web/AssetBundle.php',
  'yii\web\AssetConverter' => YII2_PATH . '/web/AssetConverter.php',
  'yii\web\AssetConverterInterface' => YII2_PATH . '/web/AssetConverterInterface.php',
  'yii\web\AssetManager' => YII2_PATH . '/web/AssetManager.php',
  'yii\web\BadRequestHttpException' => YII2_PATH . '/web/BadRequestHttpException.php',
  'yii\web\CacheSession' => YII2_PATH . '/web/CacheSession.php',
  'yii\web\CompositeUrlRule' => YII2_PATH . '/web/CompositeUrlRule.php',
  'yii\web\ConflictHttpException' => YII2_PATH . '/web/ConflictHttpException.php',
  'yii\web\Controller' => YII2_PATH . '/web/Controller.php',
  'yii\web\Cookie' => YII2_PATH . '/web/Cookie.php',
  'yii\web\CookieCollection' => YII2_PATH . '/web/CookieCollection.php',
  'yii\web\DbSession' => YII2_PATH . '/web/DbSession.php',
  'yii\web\ErrorAction' => YII2_PATH . '/web/ErrorAction.php',
  'yii\web\ErrorHandler' => YII2_PATH . '/web/ErrorHandler.php',
  'yii\web\ForbiddenHttpException' => YII2_PATH . '/web/ForbiddenHttpException.php',
  'yii\web\GoneHttpException' => YII2_PATH . '/web/GoneHttpException.php',
  'yii\web\GroupUrlRule' => YII2_PATH . '/web/GroupUrlRule.php',
  'yii\web\HeaderCollection' => YII2_PATH . '/web/HeaderCollection.php',
  'yii\web\HeadersAlreadySentException' => YII2_PATH . '/web/HeadersAlreadySentException.php',
  'yii\web\HtmlResponseFormatter' => YII2_PATH . '/web/HtmlResponseFormatter.php',
  'yii\web\HttpException' => YII2_PATH . '/web/HttpException.php',
  'yii\web\IdentityInterface' => YII2_PATH . '/web/IdentityInterface.php',
  'yii\web\JqueryAsset' => YII2_PATH . '/web/JqueryAsset.php',
  'yii\web\JsExpression' => YII2_PATH . '/web/JsExpression.php',
  'yii\web\JsonParser' => YII2_PATH . '/web/JsonParser.php',
  'yii\web\JsonResponseFormatter' => YII2_PATH . '/web/JsonResponseFormatter.php',
  'yii\web\Link' => YII2_PATH . '/web/Link.php',
  'yii\web\Linkable' => YII2_PATH . '/web/Linkable.php',
  'yii\web\MethodNotAllowedHttpException' => YII2_PATH . '/web/MethodNotAllowedHttpException.php',
  'yii\web\MultiFieldSession' => YII2_PATH . '/web/MultiFieldSession.php',
  'yii\web\MultipartFormDataParser' => YII2_PATH . '/web/MultipartFormDataParser.php',
  'yii\web\NotAcceptableHttpException' => YII2_PATH . '/web/NotAcceptableHttpException.php',
  'yii\web\NotFoundHttpException' => YII2_PATH . '/web/NotFoundHttpException.php',
  'yii\web\RangeNotSatisfiableHttpException' => YII2_PATH . '/web/RangeNotSatisfiableHttpException.php',
  'yii\web\Request' => YII2_PATH . '/web/Request.php',
  'yii\web\RequestParserInterface' => YII2_PATH . '/web/RequestParserInterface.php',
  'yii\web\Response' => YII2_PATH . '/web/Response.php',
  'yii\web\ResponseFormatterInterface' => YII2_PATH . '/web/ResponseFormatterInterface.php',
  'yii\web\ServerErrorHttpException' => YII2_PATH . '/web/ServerErrorHttpException.php',
  'yii\web\Session' => YII2_PATH . '/web/Session.php',
  'yii\web\SessionIterator' => YII2_PATH . '/web/SessionIterator.php',
  'yii\web\TooManyRequestsHttpException' => YII2_PATH . '/web/TooManyRequestsHttpException.php',
  'yii\web\UnauthorizedHttpException' => YII2_PATH . '/web/UnauthorizedHttpException.php',
  'yii\web\UnprocessableEntityHttpException' => YII2_PATH . '/web/UnprocessableEntityHttpException.php',
  'yii\web\UnsupportedMediaTypeHttpException' => YII2_PATH . '/web/UnsupportedMediaTypeHttpException.php',
  'yii\web\UploadedFile' => YII2_PATH . '/web/UploadedFile.php',
  'yii\web\UrlManager' => YII2_PATH . '/web/UrlManager.php',
  'yii\web\UrlNormalizer' => YII2_PATH . '/web/UrlNormalizer.php',
  'yii\web\UrlNormalizerRedirectException' => YII2_PATH . '/web/UrlNormalizerRedirectException.php',
  'yii\web\UrlRule' => YII2_PATH . '/web/UrlRule.php',
  'yii\web\UrlRuleInterface' => YII2_PATH . '/web/UrlRuleInterface.php',
  'yii\web\User' => YII2_PATH . '/web/User.php',
  'yii\web\UserEvent' => YII2_PATH . '/web/UserEvent.php',
  'yii\web\View' => YII2_PATH . '/web/View.php',
  'yii\web\ViewAction' => YII2_PATH . '/web/ViewAction.php',
  'yii\web\XmlResponseFormatter' => YII2_PATH . '/web/XmlResponseFormatter.php',
  'yii\web\YiiAsset' => YII2_PATH . '/web/YiiAsset.php',
  'yii\widgets\ActiveField' => YII2_PATH . '/widgets/ActiveField.php',
  'yii\widgets\ActiveForm' => YII2_PATH . '/widgets/ActiveForm.php',
  'yii\widgets\ActiveFormAsset' => YII2_PATH . '/widgets/ActiveFormAsset.php',
  'yii\widgets\BaseListView' => YII2_PATH . '/widgets/BaseListView.php',
  'yii\widgets\Block' => YII2_PATH . '/widgets/Block.php',
  'yii\widgets\Breadcrumbs' => YII2_PATH . '/widgets/Breadcrumbs.php',
  'yii\widgets\ContentDecorator' => YII2_PATH . '/widgets/ContentDecorator.php',
  'yii\widgets\DetailView' => YII2_PATH . '/widgets/DetailView.php',
  'yii\widgets\FragmentCache' => YII2_PATH . '/widgets/FragmentCache.php',
  'yii\widgets\InputWidget' => YII2_PATH . '/widgets/InputWidget.php',
  'yii\widgets\LinkPager' => YII2_PATH . '/widgets/LinkPager.php',
  'yii\widgets\LinkSorter' => YII2_PATH . '/widgets/LinkSorter.php',
  'yii\widgets\ListView' => YII2_PATH . '/widgets/ListView.php',
  'yii\widgets\MaskedInput' => YII2_PATH . '/widgets/MaskedInput.php',
  'yii\widgets\MaskedInputAsset' => YII2_PATH . '/widgets/MaskedInputAsset.php',
  'yii\widgets\Menu' => YII2_PATH . '/widgets/Menu.php',
  'yii\widgets\Pjax' => YII2_PATH . '/widgets/Pjax.php',
  'yii\widgets\PjaxAsset' => YII2_PATH . '/widgets/PjaxAsset.php',
  'yii\widgets\Spaceless' => YII2_PATH . '/widgets/Spaceless.php',
];


解析:从其运作原理看,最快找到类的方式是使用映射表。 其次,Yii中所有的类名,除了符合规范外,还需要提前注册有效的根别名

 

 类似资料: