How en when to use "Use"

Hi guys,

For the first time today I have been looking at Yii2. I have been developing with Yii for one and a half year now and so far I have lots of fun with Yii. However, i start to panic a little as soon as I see Yii 2. I installed it today to test with it, and it seems way more complicated then Yii 1 used to be. For example I created a crud with Gii and see this code:

use Yii;

use app\modules\cms\models\User;

use app\modules\cms\models\UserSearch;

use yii\web\Controller;

use yii\web\NotFoundHttpException;

use yii\filters\VerbFilter;

Can someone tell me please why I need to add all these lines? What if I make a new controller myself? How do I know what I have to add to my controller similar to the lines above? I can not seem to find an easy answer to this question in the documentation. It’s really unclear to me now.

Can someone please explain this to me?

Thanks.

The use statements are for namespaces. If you look at the controller files generated by Gii, you will see they have something like:





namespace app\controllers;




You can should be able to see that on the SiteController.php if you are using the basic app.

The use statements refer to the namespace and give you visibility on the named file.

If you were to comment out the line:





use yii\web\NotFountHttpException;






You would see that Yii 2 will complain that it can’t find the file and suggest the problem is a namespace.

You will find the NotFoundHttpException.php class in the web folder from vendor/yiisoft/yii2/web, at least that is how it is on advanced template. Open that file and you see the namespace defined at the top:





namespace yii\web;




I don’t use the basic template regularly or I could give you better examples to understand this. You just need to read up on namespaces in PHP and it will help you understand it. This might seem like an extra layer of complexity from Yii 1 to Yii 2, but it is very intuitive powerful once you get it. It’s really very simple.

When you are creating files with Gii, the namespace will generally be app\foldername, and be automatically set by Gii, unless you change it.

I feel you. Everything from the new array syntax [] to namespaces had me a little edgy at first. I’ve been at it now for about 2 months with Yii2 and am starting to really appreciate the changes now.

Here is my explanation of namespaces, using a Yii 1.1 (no namespaces) and a Yii 2.0 (with namespaces) example. Lets say I want to override a common Yii Class (CHtml in Yii 1.1, or HTML:: in Yii2).

With yii 1.1, most people tend to create another named variant of this class, like Class EHtml extends CHtml. Then, all areas of code must be updated to use the new EHtml. Find and replace. Difficult to maintain.

With Yii 2.0, you would create the class, keep it the same name. Tell php you want to ‘use’ it. Your code would stay the same, all the calls to HTML::method() would use the new class without having to update your code everywhere.

Here is a real life example, using Kartik’s gridview from my code:




use kartik\grid\GridView;

//use yii\grid\GridView;


echo GridView::widget([

        'id'=>'item-grid',

        'dataProvider' => $dataProvider,

        'filterModel' => $searchModel,

...



Is this really necessary? I think yes, eventually… you will encounter a situation where lack of namespaces will be a major annoyance. I guess it would be kind of nice if things could work by default without an explicit use statement but I’m not at the level to understand how or if that could be done.

HI guys,

Thanks for the reply and especially the understanding.

I think the help you guys gave me so far has given me a little bit of understanding on how it works now. Still have a lot to learn, but I feel a little more comfortable now with Yii 2.

Thank you!