PHP Dependency Injection

前端 未结 4 1331
一个人的身影
一个人的身影 2020-11-29 05:42

I\'m trying to get my head around Dependency Injection and I understand it, for the most part.

However, say if, for some reason, one of my classes was dependent on s

4条回答
  •  时光取名叫无心
    2020-11-29 06:02

    Dependency Injection !== DIC

    People should really stop confusing them. Dependency Injection is idea that comes from Dependency Inversion principle.

    The DIC is "magic cure", which promises to let you use dependency injection, but in PHP is usually implemented by breaking every other principle of object oriented programming. The worst implementations tend to also attach it all to global state, via static Registry or Singleton.

    Anyway, if your class depends on too many other classes, then in general , it signifies a design flaw in the class itself. You basically have a class with too many reasons to change, thus, breaking the Single Responsibility principle.

    In this case, then dependency injection container will only hide the underlaying design issues.

    If you want to learn more about Dependency Injection, i would recommend for you to watch the "Clean Code Talks" on youtube:

    • The Clean Code Talks - Don't Look For Things!
    • The Clean Code Talks - "Global State and Singletons"

提交回复
热议问题