doublel83422
doublel83422
2013-07-03 16:52

依赖注入如何知道要传递哪些变量?

  • php
  • containers
  • dependencies

I am struggling with the idea of a dependency injection container.

Not sure how to explain but I will try with a few examples:

class foo
{
    public function __construct(\Somenamespace\Bar $bar, $x)
    {

    }
}

How would the DIC know what 'x' would be?

And if we go further down the road and look at the bar class. The DIC can with no problem with reflection create Bar.

namespace \Somenamespace;
class bar
{
    public function __construct(\SomethingElse\Tap $tap, $y)
    {

    }   
}

The bar wants taps and the tap could also be found by the DIC but how does it know about 'y'?

And if we create the tap.

namespace\SomethingElse;
class tap
{
    public function __construct($amountOfTaps)
    {

    }
}

How would the DIC know about the amount of taps or whatever variable?

EDIT:

Same question does for if the hinted type is an interface or an abstract class. How does a DIC figure out what he needs to create?

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐