namespace in Yii

are there any performance differences when I use namespace outside controller vs use namespace in one of more controller methods ?




#Scenario 1


use Yii;

use app\models\Book; # the Book model is always loaded ??


class AuthorController extends Controller

{


   function actionAddBook() {

        $book = new Book();

   }

}






#Scenario 2


use Yii;




class AuthorController extends Controller

{


   function actionAddBook() {

        $book = new app\models\Book(); #Book model only loaded inside AddBook action ??

   }

}



No differences. This is just more useful to place it as "use" when you use it more than once in the code because you write less every time.

Hi!

Good question!

I’m also interested in an qualified aswer! :)

I have read some postings / articles on the internet and in summary the answer was:

  1. If there IS any performance impact from namespace / use statements, it is so little that you can ignore it.

  2. If your app is running slow you should worry MUCH more about optimizing your code instead of namespaces / use statements.

Regarding the Scenarios:

I usually use "Scenario 1" for following reasons:

  1. I’m lazy and don’t want to write the full namespace everytime.

  2. You can see what classes are used much faster by looking at the use statements at top.

  3. When the namespace changes you only have to change it once not everywhere in the code.

That are just my 2 cents.

If someone has a more qualified answer to this question please feel free to enlighten us.

Regards

Edit: Oh Bizley already answered. :)