Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection and Dependency Inversion in PHP

Dependency Injection and Dependency Inversion in PHP

Dependency Injection and Dependency Inversion are important tools for writing testable and reusable code. They are available in any object oriented language and PHP is no exception. In this talk we will look at both Dependency Injection and the Dependency Inversion Principle, how they fit in with SOLID, and why they should be used when writing object oriented code. How are objects wired together? What is an object graph? Is a Dependency Injection Container the right way forward? Can we do this automatically, and are there any patterns or reusable components available to help us achieve reusable and decoupled code? These are some of the topics covered in this talk from both a theoretical and a practical standpoint. Walking out of the room you should understand why dependency injection is so heavily advocated in programming and how you can use it to write awesome, decoupled code in PHP.

James Mallison

October 03, 2015
Tweet

More Decks by James Mallison

Other Decks in Technology

Transcript

  1. “I want a plumber who takes a pride in his

    work, who bothers to learn and gain experience, who takes what he does seriously” “I expect to pay extra for expertise, but it's a wise investment compared to hiring a cowboy plumber and having to pay later to fix up the botch job he left me with”
  2. “Integrity is "doing the right thing even when no one

    is looking", and I see Software Craftsmanship as striving for integrity in the systems we create.” David Starr (elegantcode.com)
  3. Recap • Dependency Injection is simply passing in object dependencies

    as parameters instead of instantiating them in the object using them
  4. Recap • Dependency Injection is simply passing in object dependencies

    as parameters instead of instantiating them in the object using them • Doing this gives you Inversion of Control, as the control over object creation is no longer delegated to the object using them
  5. Recap • Dependency Injection is simply passing in object dependencies

    as parameters instead of instantiating them in the object using them • Doing this gives you Inversion of Control, as the control over object creation is no longer delegated to the object using them • If you use don’t do this, other people will hate you because they can’t mock your objects easily in their tests (so your code is not testable)
  6. Recap • Dependency Injection is simply passing in object dependencies

    as parameters instead of instantiating them in the object using them • Doing this gives you Inversion of Control, as the control over object creation is no longer delegated to the object using them • Using interfaces, we can adhere to the Dependency Inversion Principle and depend on abstractions • If you use don’t do this, other people will hate you because they can’t mock your objects easily in their tests (so your code is not testable)
  7. Recap • Dependency Injection is simply passing in object dependencies

    as parameters instead of instantiating them in the object using them • Doing this gives you Inversion of Control, as the control over object creation is no longer delegated to the object using them • Using interfaces, we can adhere to the Dependency Inversion Principle and depend on abstractions • Code always starts off procedural, and builds objects up before executing them in an object oriented manner • If you use don’t do this, other people will hate you because they can’t mock your objects easily in their tests (so your code is not testable)
  8. Bootstrap (Procedural) Framework (Object Oriented) (hopefully) Dev Code (Object Oriented)

    (hopefully) Composition Root Objects built Match route to controller + action Create controller Run action with any request parameters
  9. Bootstrap (Procedural) Framework (Object Oriented) (hopefully) Dev Code (Object Oriented)

    (hopefully) Composition Root Objects built Match route to controller + action Create controller Run action with any request parameters Developer creates a controller Does whatever they want
  10. Bootstrap (Procedural) Framework (Object Oriented) (hopefully) Dev Code (Object Oriented)

    (hopefully) Composition Root Objects defined and registed in container ‘Object Graph’
  11. Bootstrap (Procedural) Framework (Object Oriented) (hopefully) Dev Code (Object Oriented)

    (hopefully) Composition Root Objects defined and registed in container Match route to controller + action Container Creates controller (with dependencies) Container runs action with any request parameters ‘Object Graph’
  12. Bootstrap (Procedural) Framework (Object Oriented) (hopefully) Dev Code (Object Oriented)

    (hopefully) Composition Root Objects defined and registed in container Match route to controller + action Container Creates controller (with dependencies) Container runs action with any request parameters Developer creates a controller DI’s registered dependencies Does whatever they want ‘Object Graph’
  13. Bootstrap (Procedural) Framework (Object Oriented) (hopefully) Dev Code (Object Oriented)

    (hopefully) Composition Root Relationships (object graph) defined in configuration file Match route to controller + action Injector Creates controller (with dependencies) Developer creates a controller DI’s registered dependencies Does whatever they want
  14. Dev Code (Object Oriented) (hopefully) Developer creates a controller DI’s

    registered dependencies Does whatever they want • Not coupled to container • Remove code / move code about at any time • Concentrate on writing SOLID code
  15. • Your objects should specify external requirements needed in their

    constructor and method signatures • Dependency Injection is simply passing in external requirements as parameters
  16. • Your objects should specify external requirements needed in their

    constructor and method signatures • Dependency Injection is simply passing in external requirements as parameters • Using interfaces for functionality that might change provides polymorphism (switch out concrete implementation any time)
  17. • Your objects should specify external requirements needed in their

    constructor and method signatures • Dependency Injection is simply passing in external requirements as parameters • Using interfaces for functionality that might change provides polymorphism (switch out concrete implementation any time) • Depending on abstractions rather than concretes (interfaces), and having your object graph composed ‘higher’ in the codebase (composition root), provides Inversion of Control and helps you adhere to the Dependency Inversion Principle
  18. How can I apply this? • Choose a framework that

    supports automatic recursive DI • Choose a micro-framework or components that allow you to substitute the ‘controller resolver’ to provide this • Legacy codebase? Don’t have to use an auto-injector, just design your interfaces well adhere to DIP when you can • In your next project, make this a primary consideration of your software architecture
  19. • The ‘gang of four book’ (GoF) • The Clean

    Coder (Robert C. Martin) • Post your code on CodeReview (and survive)