Dependency Injection for Mere Humans - Ian Littman

Learn the basics of Dependency Injection (DI) with Ian Littman, exploring topics such as types, container usage, SOLID principles, PHP improvements, and more to simplify and clean up your code.

Key takeaways
  • Traits and interfaces are used to tell the dependency injection container what to do.
  • Constructor injection is used to pass dependencies to a class, making the code cleaner.
  • Parameter injection is used to make a class dependent on an interface, not a specific implementation.
  • Inheritance is used to make a class dependent on an interface, not a specific implementation.
  • Method injection is used to make a class dependent on an interface, not a specific implementation.
  • Abstraction should not leak details.
  • The Dependency Injection Container is used to figure out how to build a class.
  • A logger is an example of a dependency.
  • A response or a response builder is an example of a dependency.
  • A mailer is an example of a dependency.
  • A user repository is an example of a dependency.
  • A language is not going to give us any help with dependencies.
  • Environment variables are dependencies.
  • Randomness is a dependency.
  • The Dependency Inversion Principle is that you depend on abstractions, not on low-level details.
  • Type safety is important when working with dependencies.
  • The SOLID principles are used to make the code better.
  • PHP 8.2 has improved type safety.
  • PHP 8.1 has constructor property promotion.
  • PHP 8.1 and above have a search method that allows you to find dependencies.
  • PHP 7.4 has a fair amount of function to support dependency injection.
  • There are different types of dependencies: constructor, method, and property.
  • Dependency injection is not used very often, but it is good to learn.