Why Yii::$app is not available in Yii3?

As written in UPGRADE.md file, Yii::$app is not available instead we need to access app variables like Yii::get(). Why it is not available? Also, What is the difference between Yii::$app and Yii::get()?

Yii::$app has been changed to Yii::getApp() in the following commit:

It was a controversial change and the discussion began. The discussion is continued in the following issue and it is still open.

The main concept behind this change, IMO, is that Yii is not a special thing any more in Yii 3. It’s just one of the helpers.

FYI, Yii and BaseYii are located at the root directory of framework in Yii 2, but they are in helpers sub directory of src in Yii 3.

I don’t have enough knowledge and experience to state my opinion on this change.

1 Like

Yii::getApp() was choosen over Yii::$app because: getApp() is method and can do instantiation of Application if it is not instantiated yet. While property cannot do that.
The application is not instantiated by default in entry script anymore.

The big change in Yii 3 is that lot of stuff is moved outside of application. And in theory framework can be used without application instantiation. In practice there is still work to do in that direction.

2 Likes

Thank you, @hiqsol.

Very interesting. A drastic design change.
Could you show us where to read the discussion among the developers about this design decision?

Unfortunately I can’t find those discussions.
At the moment there are several places of discussions: github, forum, slack. I don’t even remember where the discussion took place. I think it was github but I’m not sure.
But if you have questions we can discuss them.

1 Like