php - How can i reuse a variable more efficiently in cakePHP? -


i have code:

        $getcookiedata = $this->cookie->read('data');     $getuser = $this->user->find('first', array('conditions' => array('user.username' => $getcookiedata['username'])));  

basically i'm using in lot of locations users information , either show it, compare it, etc. wondering if there more efficient way me use variable around site, instead of repeating myself lot.

you can wrap in function:

public function getuser() {     $getcookiedata = $this->cookie->read('data');     $getuser = $this->user->find('first', array('conditions' => array('user.username' => $getcookiedata['username'])));      return $getuser; } 

and call $this->getuser(). should put in class in context of current value of $this eg in class these functions present.

you can make static (eg public static function getuser(){...}) in case able call directly using classname::getuser() syntax.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -