$application = new yii\web\Application($config);
$application->run();
yii\base\Application::__construct()
public function __construct($config = [])
{
Yii::$app = $this;
static::setInstance($this);
$this->state = self::STATE_BEGIN;
$this->preInit($config);
$this->registerErrorHandler($config);
Component::__construct($config);
var_dump($this);die; // 在这里输出 $this
}
object(yii\web\Application)#5 (33) {
......
["_modules":"yii\base\Module":private]=>
array(3) {
["debug"]=> object(yii\debug\Module)#33 (28) {
......
}
["gii"]=> object(yii\gii\Module)#113 (21) {
......
}
...
}
["_definitions":"yii\di\ServiceLocator":private]=>
array(24) {
["errorHandler"]=> .....
["request"]=> ......
["log"]=> ......
......
}
......
}
问题:object(yii\web\Application)
中 _modules
和 _definitions
中的属性是在哪里赋值的?
首先,你的 $config 数组中一定包含以下元素:
这里说明一下继承关系:
上面方法中
Component::__construct($config)
会调用yii\base\Object::__construct()
方法一、下面只是为了说明
'components' => [ 'log' => [...]]
从哪来,若不关心可以直接看 第二步。$this->preInit($config);
,即yii\base\Application::preInit(&$config)
$this->preInit($config);
, 我们得到的$config
上面只是为了说明
'components' => [ 'log' => [...]]
从哪来二、重点在这里
yii\base\Object::__construct($config = [])
中的Yii::configure($this, $config);
yii\base\Object::__set($name, $value)
当前情景下的
$object
我们可以认为是yii\base\Application
的对象$app
这里会调用
yii\base\Module::setModules($modules)
方法这样便有了问题中的
yii\di\ServiceLocator::setComponents($components)
方法这样便有了问题中的