Instantiate class within constructor

Asked

Viewed 69 times

-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?

  • 1

    "Correct" depends on each case: https://softwareengineering.stackexchange.com/q/365119

  • 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.

1 answer

2


The above code, every time the user is using the login function, for example, it will start all classes right? or am I wrong?

It will instantiate several objects of these classes within the constructor at the time of creating the object that has these methods (the question does not show this, but can only be in a class).

The correct thing would be to instantiate them within their respective functions?

It depends, the question does not say what you need. Correct is not cake recipe, correct depends on context, what you want to do. Programming is making decisions according to need. If programming would always do the same things without knowing the requirements then all programs would already have been created.

The second example of code, if the user uses Login, will start only the login. But as for the processing, this influences something?

Actually this example is confusing because it initiates the same object in two places, I doubt it makes sense. It creates a new object that already exists then if the original object has been moved and now creates a new object everything will be lost and now will have a new object.

In fact this code seems to have many conceptual problems, even without seeing the rest of the code. I can’t say anything specific because the question lacks information. Builders without parameters are usually wrong.

Many examples of OOP in PHP I see are wrong. If there is a people who do not understand OOP is that of PHP, starting with the fact that it is a language of script and it doesn’t make sense to use OOP on it. It’s true that they’re turning it into language Nterprise, but it’s half over.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.