-2
If I instantiate several classes in a constructor, for example:
public function __construct()
{
parent::__construct();
$this->loginModel = new \Application\Models\Login;
$this->followModel = new \Application\Models\Follow;
$this->likeModel = new \Application\Models\Like;
$this->commentModel = new \Application\Models\Comment;
}
public function login()
{
#code
}
public function follow()
{
#code
}
public function like()
{
#code
}
public function comment()
{
#code
}
The above code, every time the user is using the login function, for example, it will start all classes right? Or am I wrong?
The correct thing would be to instantiate them within their respective functions?
public function __construct()
{
parent::__construct();
$this->loginModel = new \Application\Models\Login;
}
public function login()
{
$this->loginModel = new \Application\Models\Login;
}
The second example of code, if the user uses the login, will start only the login
. But as for processing, it influences something?
"Correct" depends on each case: https://softwareengineering.stackexchange.com/q/365119
– hkotsubo
The negative is perhaps because the question is not very clear, you say you do not have a Crsital ball, but the others also do not. So if you think others should write something else do their part before criticizing. The negative is a criticism, people are tired of people making the same mistakes all the time and don’t comment anymore. If you have already taken several negatives try harder, read and reread the question several times, put yourself in the place of those who read and see if you have everything she needs. Don’t assume anything, after all you have things in your head that the other person doesn’t have, so write.
– Maniero