I have code that often looks like this:
private $user;
public function __construct()
{
$this->user = User::getInstance();
}
public function methodOne()
{
return $this->user->foo();
}
public function methodTwo()
{
return $this->user->foo2();
}
public function methodThree()
{
return $this->user->foo3();
}
I believe that if I set the user property of the instance, I can reuse the shorter name in my methods (well, in this case, it is not much shorter). I also thought that doing it this way could save some resources (start to doubt it), but when I look at the code of other people, I rarely see how people do it. Usually they just call:
User::getInstance()->foo();
User::getInstance()->foo2();
User::getInstance()->foo3();
Is there any best practice for this? Maybe if this is not one class, can you do it like this? Or maybe you shouldn't do that? Hope to get clarification, thanks.
Edit: If there is any misunderstanding, I'm just wondering if I should create a property to store the vs this instance in the first example:
public function methodOne()
{
return User::getInstance()->foo();
}
public function methodTwo()
{
return User::getInstance()->foo2();
}
public function methodThree()
{
return User::getInstance()->foo3();
}
, , , ...