关于 PHP 中依赖注入的查询

发布于 2024-12-20 13:46:09 字数 792 浏览 1 评论 0原文

我一直在阅读依赖注入,我想我非常理解这些概念,包括构造函数注入和设置器注入。

但是,我不确定在以下情况下我会做什么:

假设我有一个包含许多方法的类,但其中只有一个方法需要某个其他对象。我该如何将此对象提供给该方法?我是否应该直接将对象作为参数传递给方法,如下所示:

class aClass
{
    function aMethod(anotherClass $anotherClass)
    {
        anotherClass->hello();
    }

    function otherMethods()
    {
    }
    .....
}

另一个选择是将其提供给整个类,这似乎没有必要。像这样:

class aClass
{
    protected $_anotherClass;

    function aMethod()
    {
        anotherClass->hello();
    }

    function otherMethods()
    {
    }
    .....

    function setAnotherClass(anotherClass $anotherClass)
    {
            $this->_anotherClass;
    }
}

什么时候应该将对象传递给方法以及什么时候应该通过构造函数/设置器使用依赖注入? 是否有关于何时使用这两个选项之一的经验法则或最佳实践?

提前感谢您的帮助。

I've been reading up on dependency injection and I think I pretty much understand the concepts, including constructor injection and setter injection.

However, I am unsure what I would do in the following scenario:

Say I have a class that has many methods, but only one of those methods requires a certain other object. How do I go about providing this object to the method? Should I not just directly pass the object to method as a parameter like so:

class aClass
{
    function aMethod(anotherClass $anotherClass)
    {
        anotherClass->hello();
    }

    function otherMethods()
    {
    }
    .....
}

The other option would be to provide it to the entire class, which seems unnecessary. like so:

class aClass
{
    protected $_anotherClass;

    function aMethod()
    {
        anotherClass->hello();
    }

    function otherMethods()
    {
    }
    .....

    function setAnotherClass(anotherClass $anotherClass)
    {
            $this->_anotherClass;
    }
}

When should I pass an object to a method and when should I use dependency injection via constructors/setters?
Is there a rule of thumb or a best practice for when to use one of these two options?

Thanks for your help in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

踏月而来 2024-12-27 13:46:09

您提到了一条经验法则,所以这里是:

问自己一个问题:有人可以合法地对同一个对象调用此方法两次,并为两次调用中的参数提供不同的值吗?

如果是,则将其保留为参数。如果不是,那么正如您所描述的那样,它是注射的良好候选者。

这是第一种方法的一个很好的示例,其中删除参数并在类中注入依赖项将是一个非常糟糕的主意。 C# 而不是 PHP,但你明白了。

You mentioned a rule of thumb, so here it is:

Ask yourself the question: could someone legitimately call this method twice on the same object, providing different values for the argument in the two calls?

If yes, then leave it as a parameter. If no, then it's a good candidate for injection as you describe.

This is a good example of a method of the first type, where removing the parameter and injecting a dependency in the class instead would be a really bad idea. C# instead of PHP, but you get the picture.

独木成林 2024-12-27 13:46:09

类的内部运作是类本身的事,与其他人无关。如果只有一个函数需要依赖关系,那就这样吧。

或者,您可能想知道为什么只有一个函数具有依赖性,您的类是否可能在做两件单独的事情?

您不想在构造函数中注入依赖项的唯一原因是,制作该资源的成本非常高(在磁盘上准备好一个大文件,连接到远程主机等),但即使在这种情况下,您也可以通过在“代理”或“延迟”对象中作为“承诺”,如果您需要它,您可以获取依赖项。

The internal workings of a class are no-ones business but that class itself. If only one function requires the dependency then so be it.

Alternatively you could wonder why only one function has the dependency, is you class maybe doing two separate things?

The only reason you wouldn't want to inject dependencies in the constructor is when the cost of making that resource is very high (ready a large file on disk, connecting to remote host etc.), but even in that case, you could pass in a 'proxy' or 'deferred' object as a 'promise' that you can get to the dependency if you ever need it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文