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. Dependency Injection and
    Dependency Inversion in PHP
    @J7mbo

    View full-size slide

  2. Your goal on a project?

    View full-size slide

  3. Professional Integrity

    View full-size slide

  4. “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”

    View full-size slide

  5. “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)

    View full-size slide

  6. A little bit about me…

    View full-size slide

  7. OOP, DI, DiP, IoC, DIC, SOLID, WTF

    View full-size slide

  8. Object Instantiation

    View full-size slide

  9. Object Instantiation
    Http
    Request

    View full-size slide

  10. Object Instantiation
    Http
    Request
    Disgusting
    DOM Searching

    View full-size slide

  11. Object Instantiation
    Http
    Request
    Disgusting
    DOM Searching
    Sorting Algorithm

    View full-size slide

  12. Please be True…

    View full-size slide

  13. Asking for objects to be passed in
    is Dependency Injection
    Calling Code

    View full-size slide

  14. Inversion of Control

    View full-size slide

  15. I have to mock this…
    And this… This too.. srsly

    View full-size slide

  16. Calling getContents()
    will now return
    this HTML
    Passing in my
    $fakeClient

    View full-size slide

  17. Dependency Injection
    provides Control

    View full-size slide

  18. 1 year later…

    View full-size slide

  19. “High-level modules should not depend on low-level modules.
    Both should depend on abstractions”

    View full-size slide

  20. “Polymorphism is the provision of a single interface
    to entities of different types”

    View full-size slide

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

    View full-size slide

  22. 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

    View full-size slide

  23. 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)

    View full-size slide

  24. 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)

    View full-size slide

  25. 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)

    View full-size slide

  26. Bootstrap
    (Procedural)
    Framework
    (Object Oriented)
    (hopefully)
    Dev Code
    (Object Oriented)
    (hopefully)
    Composition Root
    Objects built

    View full-size slide

  27. 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

    View full-size slide

  28. 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

    View full-size slide

  29. Framework
    (Object Oriented)
    (hopefully)

    View full-size slide

  30. Framework
    (Object Oriented)
    (hopefully)

    View full-size slide

  31. Dependency Injection Container
    (DiC)

    View full-size slide

  32. Get the Conference
    object here

    View full-size slide

  33. External requirements should be visible from
    an object’s constructor and method signatures

    View full-size slide

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

    View full-size slide

  35. 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’

    View full-size slide

  36. 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’

    View full-size slide

  37. Create the
    PhpNw15Conference object

    View full-size slide

  38. Automatic Injection for concrete classes

    View full-size slide

  39. See ‘Conference’
    Create ‘PhpNw15Conference’

    View full-size slide

  40. 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

    View full-size slide

  41. Dev Code
    (Object Oriented)
    (hopefully)
    Developer creates
    a controller
    DI’s registered
    dependencies
    Does whatever they want

    View full-size slide

  42. Dev Code
    (Object Oriented)
    (hopefully)
    Developer creates
    a controller
    DI’s registered
    dependencies
    Does whatever they want

    View full-size slide

  43. 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

    View full-size slide

  44. • Your objects should specify external requirements needed in their
    constructor and method signatures

    View full-size slide

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

    View full-size slide

  46. • 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)

    View full-size slide

  47. • 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

    View full-size slide

  48. What next?
    AOP and PHP7’s Anonymous Classes

    View full-size slide

  49. ‘Decorate’ existing Controller
    Delegate controller
    method call,

    but add additional
    functionality (logging)

    View full-size slide

  50. Anonymous Class
    Automatic

    Decoration

    View full-size slide

  51. 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

    View full-size slide

  52. • The ‘gang of four book’ (GoF)
    • The Clean Coder (Robert C. Martin)
    • Post your code on CodeReview
    (and survive)

    View full-size slide

  53. @J7mbo
    https://joind.in/talk/view/15431

    View full-size slide