我应该在构造函数中实例化其他类吗?
最近,我看到我的一位同事在构造函数中实例化他的类,所以我开始做同样的事情,如下所示:
class FooBar{
private $model1;
private $model2;
public function __construct() {
$this->model1=new Model1();
$this->model2=new Model2();
}
}
现在我开始想,是否在需要模型的任何地方实例化模型可能会更好?
例如,函数 foo()
需要 model1,函数 bar()
需要 model2,但现在两个模型都已加载。
那么问题来了:这是实例化其他类的正确方法吗?或者我应该在函数中需要它们时实例化它们?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嗯,一如既往,没有一刀切的答案。
大多数时候,类
FooBar
聚合$model1
和$model2
因为它需要它们来实现其功能。在这种情况下,FooBar
不能做太多事情,除非它在这些变量中包含对象,因此在构造函数中创建它们是正确的做法。有时不需要聚合对象来执行
FooBar
类的大部分功能,并且该对象的构造是一项昂贵的操作。在这种情况下,仅根据需要使用如下代码构建它是有意义的:但是,这只是有时。如果
FooBar
类的一半操作需要$model1
,另一半操作需要$model2
,这可能表明FooBar
code> 遇到了“让我们把所有东西都放在一个类中”的情况,应该分成两个类。Well, as always there is no one size fits all answer.
Most of the time, class
FooBar
aggregates$model1
and$model2
because it needs them to fulfill its function. In this scenario there's not much thatFooBar
can do unless it has objects in these variables, so it's the right thing to do to create them in the constructor.Sometimes an aggregate object is not needed to perform a large part of class
FooBar
's function, and the construction of that object is an expensive operation. In this case, it makes sense to only construct it on demand with code like the following:However, that's only sometimes. If class
FooBar
needs$model1
for half of its operations and$model2
for the other half, this may indicate thatFooBar
is suffering from a case of "let's throw everything inside one class" and should be split into two classes instead.我希望看到这些依赖项作为参数注入到构造函数中。
I would like to see these dependencies injected into the constructor as parameters.
实际上,您应该在需要时加载它们,否则每次您需要完成一个简单的操作时,一大堆不需要的模型(可能有自己的构造函数,并加载更多模型!)将弹出到内存中。
不要创建新模型,除非您确定将使用它们(例如需要本地化的模型等)
You should actually be loading them when you need them otherwise a whole bunch of models that are not required (which may have their own constructors with more models loading!) will pop into memory every time you need a trivial operation done.
Don't create a new model unless you're sure you will be using them (e.g. models needed to localize and such)
这不是精确的科学,您应该遵循自己的直觉来组织代码。
如果这种方法无法维护,或者您想对其进行单元测试,依赖注入可能会派上用场。
但是,如果您正在编写简单的脚本并且开发时间是一个重要因素,那么您现在的做法就足够了。
It is not exact science, and you should follow your instincts in how to organize the code.
If this approach gets unmaintainable, or you want to unit test it, dependency injection might come to the rescue.
But if you're doing simple scripts and development time is an important factor, the way you're doing it now is sufficient.