Dependency-injection in real life

后端 未结 3 1429
温柔的废话
温柔的废话 2021-01-31 19:10

I am building a really minimal MVC framework to increase my PHP knowledge and challenge myself. I\'ve come to the point where Classes begin to be dependent on each other to work

3条回答
  •  无人共我
    2021-01-31 20:06

    Making one big container including every possible class that may be needed seems nothing but counter-productive to me and I can't imagine that it is good practice. It seems like the recipe for bad performance at least.

    On the contrary. This is exactly what you would do with a di container. The container will only instantiate objects on demand, so there is virtually no overhead to managing all you singleton-ish classes through it.

    The biggest problem with di is to distinguish between shared objects (Things you would usually think of as singletons) and transient objects (Objects that have plenty of instances through a normal application flow.). The former are easily managed through di. The latter don't really fit. Having those two "kinds" of objects clearly distinguished may seem like a bit of a hassle, but is really a highly beneficial side effect of using a di container.

提交回复
热议问题