Recently, I saw a colleague of mine instantiate his classes in a constructor, so I started doing the same, like this:
class FooBar{
private $model1;
private $model2;
public function __construct() {
$this->model1=new Model1();
$this->model2=new Model2();
}
}
And now I'm starting to wonder, if maybe instantiating the models everywhere where they are needed may be better?
E.g., function foo()
needs model1 and function bar()
needs model2, but now both models are loaded.
So, the question: Is this the right way to instantiate other classes? Or should I just instantiate them when I need them in a function?