Calling vs. Storing Yii::app()->user

Okay, this one gets down to the nitty-gritty details of what makes Yii work.

It seems to be pretty common practice to store the user entity in a variable like so:


$user = Yii::app()->user;

And that makes sense. The question is, how often should this be done? Does it make more sense to get and store the user entity once in each function where it’s needed, like so:




public function foo() {

	$user = Yii::app()->user;

	/* Code here */

	$this->bar();

}

private function bar() {

	$user = Yii::app()->user;

	/* Code here */

}



Or, should I keep and pass the $user variable, like so:




public function foo() {

	$user = Yii::app()->user;

	/* Code here */

	$this->bar($user);

}

private function bar($user) {

	/* Code here */

}



What are the advantages and disadvantages, as far as memory and speed go?

This all depends upon your need.

If you have a function which needs some other functions to work and the intended functions does not need to have any predefined values then you can follow the first approach else second one will fulfill the need.

One more, you can also use null values and before using them, you can check whither these are set of not? But still you need to access the User Object to check and compare.