Home >Backend Development >PHP Tutorial >Yii2中 Yii::$app $app这个变量是在哪里实例化的?

Yii2中 Yii::$app $app这个变量是在哪里实例化的?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 20:34:051754browse

最常用的服务定位器是application(应用)对象,可以通过 \Yii::$app 访问

回复内容:

最常用的服务定位器是application(应用)对象,可以通过 \Yii::$app 访问

看下源码就知道了,如果你的应用是一个web应用,其index.php中
(new yii\web\Application($config))->run();
调用Application的构造方法,而其继承自yii\base\Application构造方法为

<code>public function __construct($config = [])
{
    Yii::$app = $this;
    $this->setInstance($this);
    $this->state = self::STATE_BEGIN;
    $this->preInit($config);
    $this->registerErrorHandler($config);
    Component::__construct($config);
}
</code>

具体往后的实例化过程,你可以按照找个顺序继续查看源码。有什么问题可以留言,查看源码可以帮助你迅速理解yiiframework的初始化及运行过程

这是个神奇的变量!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn