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

Dependency Injection with PHP

Dependency Injection with PHP

8e82eb7e128a14a16d642ae55227339b?s=128

Bastian Hofmann

October 16, 2012
Tweet

More Decks by Bastian Hofmann

Other Decks in Programming

Transcript

  1. Dependency Injection with PHP @BastianHofmann

  2. None
  3. None
  4. None
  5. ?

  6. Dependency Injection Dependency Injection Containers Dependency Inversion Dependencies

  7. None
  8. None
  9. ResearchGate gives science back to the people who make it

    happen. We help researchers build reputation and accelerate scientific progress. On their terms. ‟
  10. None
  11. None
  12. None
  13. None
  14. None
  15. None
  16. None
  17. Questions? Ask!

  18. http://speakerdeck.com/u/bastianhofmann

  19. Dependency Inversion

  20. http://www.doolwind.com/blog/solid-principles-for-game-developers/

  21. http://qafoo.com/presentations.html

  22. http://www.doolwind.com/blog/solid-principles-for-game-developers/

  23. High-level modules should not depend on low level modules but

    on abstractions. ”
  24. A B C

  25. A B C <<interface>> <<interface>>

  26. A B C <<interface>>

  27. A B Adapter <<interface>> C

  28. None
  29. None
  30. Dependency Injection

  31. Constructor

  32. None
  33. None
  34. Setter

  35. None
  36. Method argument

  37. None
  38. Constructing classes

  39. None
  40. Benefits

  41. None
  42. Easier to change

  43. Easier testing

  44. Only test the class, not the dependencies

  45. None
  46. Mocking

  47. None
  48. None
  49. http://www.doolwind.com/blog/solid-principles-for-game-developers/

  50. But...

  51. None
  52. Make it easier with factories

  53. None
  54. Dependency Injection Container

  55. None
  56. rg\injektor inspired by google-guice

  57. https://github.com/researchgate/injektor

  58. http://getcomposer.org/ http://packagist.org/

  59. $ cat composer.json

  60. $ wget http://getcomposer.org/composer.phar $ php composer.phar install Installing dependencies -

    Installing monolog/monolog (1.1.0) Downloading: 100% ... monolog/monolog suggests installing mlehner/ gelf-php (Allow sending log messages to a GrayLog2 server) Writing lock file Generating autoload files
  61. None
  62. Creating an instance of a class

  63. None
  64. None
  65. None
  66. PHP 5.5

  67. Automatic creation of dependencies through Typehints

  68. None
  69. @inject

  70. None
  71. What classes can be injected?

  72. No constructor

  73. None
  74. Constructor without arguments

  75. None
  76. Constructor with arguments that have default values

  77. None
  78. Injectable constructor

  79. None
  80. None
  81. By the way: Property Injection

  82. None
  83. Singletons with public static getInstance method following the same rules

  84. None
  85. STOP: Don‘t use Singeltons

  86. Singletons are evil!

  87. Let the DIC take care of instances

  88. @singleton

  89. None
  90. Passing additional, fixed values to instance

  91. None
  92. Working with Interfaces

  93. None
  94. But we can not create an instance of an Interface!

  95. @implementedBy

  96. None
  97. Named @implementedBy

  98. None
  99. None
  100. Providers

  101. @providedBy

  102. None
  103. None
  104. Method invocation at DIC

  105. None
  106. Same rules

  107. None
  108. None
  109. And if you don‘t like annotations... ... or want to

    integrate 3rd party code
  110. None
  111. None
  112. Great! So, how does it work?

  113. Development: Reflection

  114. Production: Generated Factory Classes

  115. None
  116. None
  117. https://github.com/bashofmann/dic_demo/

  118. DEMO

  119. None
  120. https://github.com/symfony/DependencyInjection

  121. None
  122. None
  123. None
  124. https://github.com/bashofmann/dic_demo/

  125. DEMO

  126. None
  127. https://github.com/zendframework/zf2

  128. None
  129. None
  130. Automatic creation of dependencies through Typehints

  131. https://github.com/bashofmann/dic_demo/

  132. DEMO

  133. AOP

  134. http://flow3.typo3.org/documentation/guide/partiii/ aspectorientedprogramming.html

  135. class  Forum  {    /**      *  @FLOW3\Inject  

       *  @var  ApplicationLoggerInterface      */    protected  $applicationLogger;    /**      *  @param  Post  $post      *  @return  void      */    public  function  deletePost(Post  $post)  {        $this-­‐>applicationLogger-­‐>log('Removing  post');        $this-­‐>posts-­‐>remove($post);    } }
  136. class  Forum  {    /**      *  Delete  a

     forum  post      *      *  @param  Post  $post      *  @return  void      */    public  function  deletePost(Post  $post)  {        $this-­‐>posts-­‐>remove($post);    } }
  137. /**  *  @FLOW3\Aspect  */ class  LoggingAspect  {    /**  

       *  @FLOW3\Inject      *  @var  ApplicationLoggerInterface      */    protected  $applicationLogger;    /**      *  @FLOW3\Before("method(Forum-­‐>deletePost())")      */    public  function  logDeletePost(\TYPO3\FLOW3\AOP \JoinPointInterface  $joinPoint)  {        $post  =  $joinPoint-­‐>getMethodArgument('post');        $this-­‐>applicationLogger-­‐>log('Removing');    } }
  138. None
  139. h"p://twi"er.com/Bas2anHofmann h"p://profiles.google.com/bashofmann h"p://lanyrd.com/people/Bas2anHofmann h"p://speakerdeck.com/u/bas2anhofmann h"ps://github.com/bashofmann mail@bas2anhofmann.de Did you like this

    talk? https://joind.in/7346