The state of DI in PHP
The state of DI in PHP About me  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  enjoying PHP since...
The state of DI in PHP What`s this talk about?
The state of DI in PHP What`s this talk about?      No framework bashing! Hopefully :)
The state of DI in PHP What`s this talk about?                  It`s all about how                  DI is implemented.
The state of DI in PHP What`s DI about?
The state of DI in PHP Inversion of control
The state of DI in PHP „Hollywood principle“
The state of DI in PHP What`s DI about?  new TalkService(new TalkRepository());
The state of DI in PHP What`s DI about?    Consumer
The state of DI in PHP What`s DI about?    Consumer             Dependencies
The state of DI in PHP What`s DI about?    Consumer             Dependencies   Container
The state of DI in PHP What`s DI about?    Consumer             Dependencies   Container
The state of DI in PHP A little bit of history...
The state of DI in PHP 1988         „Designing Reusable Classes“                   Ralph E. Johnson & Brian Foote
The state of DI in PHP 1996  „The Dependency Inversion Principle“                         Robert C. Martin
The state of DI in PHP 2002
The state of DI in PHP 2003        Spring Framework 0.9 released
The state of DI in PHP 2004   „Inversion of Control Containers and    the Dependency Injection pattern“                   ...
The state of DI in PHP 2004       Spring Framework 1.0 released
The state of DI in PHP 2004                   PHP 5.0 released
The state of DI in PHP 2005 / 2006               Garden, a lightweight               DI container for PHP.
The state of DI in PHP 2006                First PHP5 framework                    with DI support
The state of DI in PHP 2007    International PHP Conference 2007         features 2 talks about DI.
The state of DI in PHP 2009                  PHP 5.3.0 released
The state of DI in PHP 2010         Fabien Potencier talks about          „Dependency Injection in              PHP 5.2 an...
The state of DI in PHP 2011               Symfony2, Flow3,            Zend Framework 2 (beta)
The state of DI in PHP 2012                         And now?
The state of DI in PHP DI has finally arrived in the PHP world!
The state of DI in PHP            Let`s have a look at the           different implementations!
The state of DI in PHP
The state of DI in PHP ZendDi – First steps <?php namespace Acme; class TalkService {     public function __construct() { ...
The state of DI in PHP ZendDi – First steps <?php $di = new ZendDiDi(); $service = $di->get(AcmeTalkService); $service->ge...
The state of DI in PHP ZendDi – Constructor Injection <?php namespace Acme; interface GenericRepository {     public funct...
The state of DI in PHP ZendDi – Constructor Injection <?php $di = new ZendDiDi(); $service = $di->get(AcmeTalkService); $s...
The state of DI in PHP ZendDi – Setter Injection <?php namespace Acme; class Logger {     public function doLog($logMsg) {...
The state of DI in PHP ZendDi – Setter Injection <?php $di = new ZendDiDi(); $di->configure(     new ZendDiConfiguration( ...
The state of DI in PHP ZendDi – Interface Injection <?php namespace Acme; class Logger {     public function doLog($logMsg...
The state of DI in PHP ZendDi – Interface Injection <?php $di = new ZendDiDi(); $service = $di->get(AcmeTalkService); $ser...
The state of DI in PHP ZendDi – General usage <?php $di = new ZendDiDi(); $service = $di->get(AcmeTalkService); var_dump($...
The state of DI in PHP ZendDi – Builder Definition <?php // describe dependency $dep = new ZendDiDefinitionBuilderPhpClass...
The state of DI in PHP ZendDi – Builder Definition <?php // add to Di $defList = new ZendDiDefinitionList($builder); $di =...
The state of DI in PHP
The state of DI in PHP Symfony2 <?php namespace AcmeTalkBundleController; use SymfonyBundleFrameworkBundleControllerContro...
The state of DI in PHP Symfony2 – Configuration file File services.xml in src/Acme/DemoBundle/Resources/config <?xml versi...
The state of DI in PHP Symfony2 – Constructor Injection <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema...
The state of DI in PHP Symfony2 – Setter Injection <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/...
The state of DI in PHP Symfony2 – Setter Injection (optional) <?xml version="1.0" ?> <container xmlns="http://symfony.com/...
The state of DI in PHP Symfony2 – Property Injection <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/di...
The state of DI in PHP Symfony2 – private/public Services <?xml version="1.0" ?> <container xmlns="http://symfony.com/sche...
The state of DI in PHP Symfony2 – Service inheritance <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/d...
The state of DI in PHP Symfony2 – Service scoping <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/s...
The state of DI in PHP
The state of DI in PHP Flow3 – Constructor Injection <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW...
The state of DI in PHP Flow3 – Constructor Injection (manually) <?php namespace AcmeDemoController; use TYPO3FLOW3Annotati...
The state of DI in PHP Flow3 – Setter Injection (manually) <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations a...
The state of DI in PHP Flow3 – Setter Injection (manually) File Objects.yaml in Packages/Application/Acme.Demo/Configurati...
The state of DI in PHP Flow3 – Setter Injection (Automagic) <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations ...
The state of DI in PHP Flow3 – Setter Injection (Automagic) <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations ...
The state of DI in PHP Flow3 – Property Injection <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; ...
The state of DI in PHP Flow3 – Property Injection (with Interface) <?php namespace AcmeDemoController; use TYPO3FLOW3Annot...
The state of DI in PHP Flow3 – Property Injection (with Interface) File Objects.yaml in Packages/Application/Acme.Demo/Con...
The state of DI in PHP Flow3 – Scoping <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; /**  * @FLO...
The state of DI in PHP The future?
The state of DI in PHP The future (or what`s missing)?
The state of DI in PHP PSR for DI container missing!
The state of DI in PHP IDE support is missing...
Thank you!
http://joind.in/4768
Upcoming SlideShare
Loading in...5
×

The state of DI in PHP - phpbnl12

3,028

Published on

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,028
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
23
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

The state of DI in PHP - phpbnl12

  1. 1. The state of DI in PHP
  2. 2. The state of DI in PHP About me  Stephan Hochdörfer, bitExpert AG  Department Manager Research Labs  enjoying PHP since 1999  7 years of DI experience (in PHP)  S.Hochdoerfer@bitExpert.de  @shochdoerfer
  3. 3. The state of DI in PHP What`s this talk about?
  4. 4. The state of DI in PHP What`s this talk about? No framework bashing! Hopefully :)
  5. 5. The state of DI in PHP What`s this talk about? It`s all about how DI is implemented.
  6. 6. The state of DI in PHP What`s DI about?
  7. 7. The state of DI in PHP Inversion of control
  8. 8. The state of DI in PHP „Hollywood principle“
  9. 9. The state of DI in PHP What`s DI about? new TalkService(new TalkRepository());
  10. 10. The state of DI in PHP What`s DI about? Consumer
  11. 11. The state of DI in PHP What`s DI about? Consumer Dependencies
  12. 12. The state of DI in PHP What`s DI about? Consumer Dependencies Container
  13. 13. The state of DI in PHP What`s DI about? Consumer Dependencies Container
  14. 14. The state of DI in PHP A little bit of history...
  15. 15. The state of DI in PHP 1988 „Designing Reusable Classes“ Ralph E. Johnson & Brian Foote
  16. 16. The state of DI in PHP 1996 „The Dependency Inversion Principle“ Robert C. Martin
  17. 17. The state of DI in PHP 2002
  18. 18. The state of DI in PHP 2003 Spring Framework 0.9 released
  19. 19. The state of DI in PHP 2004 „Inversion of Control Containers and the Dependency Injection pattern“ Martin Fowler
  20. 20. The state of DI in PHP 2004 Spring Framework 1.0 released
  21. 21. The state of DI in PHP 2004 PHP 5.0 released
  22. 22. The state of DI in PHP 2005 / 2006 Garden, a lightweight DI container for PHP.
  23. 23. The state of DI in PHP 2006 First PHP5 framework with DI support
  24. 24. The state of DI in PHP 2007 International PHP Conference 2007 features 2 talks about DI.
  25. 25. The state of DI in PHP 2009 PHP 5.3.0 released
  26. 26. The state of DI in PHP 2010 Fabien Potencier talks about „Dependency Injection in PHP 5.2 and 5.3“
  27. 27. The state of DI in PHP 2011 Symfony2, Flow3, Zend Framework 2 (beta)
  28. 28. The state of DI in PHP 2012 And now?
  29. 29. The state of DI in PHP DI has finally arrived in the PHP world!
  30. 30. The state of DI in PHP Let`s have a look at the different implementations!
  31. 31. The state of DI in PHP
  32. 32. The state of DI in PHP ZendDi – First steps <?php namespace Acme; class TalkService { public function __construct() { } public function getTalks() { } }
  33. 33. The state of DI in PHP ZendDi – First steps <?php $di = new ZendDiDi(); $service = $di->get(AcmeTalkService); $service->getTalks();
  34. 34. The state of DI in PHP ZendDi – Constructor Injection <?php namespace Acme; interface GenericRepository { public function readTalks(); } class TalkRepository implements GenericRepository { public function readTalks() { } } class TalkService { public function __construct(TalkRepository $repo) { } public function getTalks() { } }
  35. 35. The state of DI in PHP ZendDi – Constructor Injection <?php $di = new ZendDiDi(); $service = $di->get(AcmeTalkService); $service->getTalks();
  36. 36. The state of DI in PHP ZendDi – Setter Injection <?php namespace Acme; class Logger { public function doLog($logMsg) { } } class TalkService { public function __construct(TalkRepository $repo) { } public function setLogger(Logger $logger) { } public function getTalks() { } }
  37. 37. The state of DI in PHP ZendDi – Setter Injection <?php $di = new ZendDiDi(); $di->configure( new ZendDiConfiguration( array( definition => array( class => array( AcmeTalkService => array( setLogger => array(required => true) ) ) ) ) ) ); $service = $di->get(AcmeTalkService); var_dump($service);
  38. 38. The state of DI in PHP ZendDi – Interface Injection <?php namespace Acme; class Logger { public function doLog($logMsg) { } } interface LoggerAware { public function setLogger(Logger $logger); } class TalkService implements LoggerAware { public function __construct(TalkRepository $repo) { } public function setLogger(Logger $logger) { } public function getTalks() { } }
  39. 39. The state of DI in PHP ZendDi – Interface Injection <?php $di = new ZendDiDi(); $service = $di->get(AcmeTalkService); $service->getTalks();
  40. 40. The state of DI in PHP ZendDi – General usage <?php $di = new ZendDiDi(); $service = $di->get(AcmeTalkService); var_dump($service); $service2 = $di->get(AcmeTalkService); var_dump($service2); // same instance as $service $service3 = $di->get( AcmeTalkService, array( repo => new phpbnl12TalkRepository() ) ); var_dump($service3); // new instance
  41. 41. The state of DI in PHP ZendDi – Builder Definition <?php // describe dependency $dep = new ZendDiDefinitionBuilderPhpClass(); $dep->setName(AcmeTalkRepository); // describe class $class = new ZendDiDefinitionBuilderPhpClass(); $class->setName(AcmeTalkService); // add injection method $im = new ZendDiDefinitionBuilderInjectionMethod(); $im->setName(__construct); $im->addParameter(repo, AcmeTalkRepository); $class->addInjectionMethod($im); // configure builder $builder = new ZendDiDefinitionBuilderDefinition(); $builder->addClass($dep); $builder->addClass($class);
  42. 42. The state of DI in PHP ZendDi – Builder Definition <?php // add to Di $defList = new ZendDiDefinitionList($builder); $di = new ZendDiDi($defList); $service = $di->get(AcmeTalkService); var_dump($service);
  43. 43. The state of DI in PHP
  44. 44. The state of DI in PHP Symfony2 <?php namespace AcmeTalkBundleController; use SymfonyBundleFrameworkBundleControllerController; use SensioBundleFrameworkExtraBundleConfigurationRoute; use SensioBundleFrameworkExtraBundleConfigurationTemplate; class TalkController extends Controller { /** * @Route("/", name="_talk") * @Template() */ public function indexAction() { $service = $this->get(acme.talk.service); return array(); } }
  45. 45. The state of DI in PHP Symfony2 – Configuration file File services.xml in src/Acme/DemoBundle/Resources/config <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> </container>
  46. 46. The state of DI in PHP Symfony2 – Constructor Injection <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="acme.talk.repo" class="AcmeTalkBundleServiceTalkRepository" /> <service id="acme.talk.service" class="AcmeTalkBundleServiceTalkService"> <argument type="service" id="acme.talk.repo" /> </service> </services> </container>
  47. 47. The state of DI in PHP Symfony2 – Setter Injection <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="acme.talk.logger" class="AcmeTalkBundleServiceLogger" /> <service id="acme.talk.repo" class="AcmeTalkBundleServiceTalkRepository" /> <service id="acme.talk.service" class="AcmeTalkBundleServiceTalkService"> <argument type="service" id="acme.talk.repo" /> <call method="setLogger"> <argument type="service" id="acme.talk.logger" /> </call> </service> </services> </container>
  48. 48. The state of DI in PHP Symfony2 – Setter Injection (optional) <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="acme.talk.logger" class="AcmeTalkBundleServiceLogger" /> <service id="acme.talk.repo" class="AcmeTalkBundleServiceTalkRepository" /> <service id="acme.talk.service" class="AcmeTalkBundleServiceTalkService"> <argument type="service" id="acme.talk.repo" /> <call method="setLogger"> <argument type="service" id="acme.talk.logger" on-invalid="ignore" /> </call> </service> </services> </container>
  49. 49. The state of DI in PHP Symfony2 – Property Injection <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="acme.talk.repo" class="AcmeTalkBundleServiceTalkRepository" /> <service id="acme.talk.service" class="AcmeTalkBundleServiceTalkService"> <property name="talkRepository" type="service" id="acme.talk.repo" /> </service> </services> </container>
  50. 50. The state of DI in PHP Symfony2 – private/public Services <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="acme.talk.logger" class="AcmeTalkBundleServiceLogger" public="false" /> <service id="acme.talk.repo" class="AcmeTalkBundleServiceTalkRepository" /> <service id="acme.talk.service" class="AcmeTalkBundleServiceTalkService"> <argument type="service" id="acme.talk.repo" /> <call method="setLogger"> <argument type="service" id="acme.talk.logger" /> </call> </service> </services> </container>
  51. 51. The state of DI in PHP Symfony2 – Service inheritance <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="acme.talk.serviceparent" class="AcmeTalkBundleServiceTalkService" abstract="true"> <property name="talkRepository" type="service" id="acme.talk.repo" /> </service> <service id="acme.talk.service" parent="acme.talk.serviceparent" /> <service id="acme.talk.service2" parent="acme.talk.serviceparent" /> </services> </container>
  52. 52. The state of DI in PHP Symfony2 – Service scoping <?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> <services> <service id="acme.talk.repo" class="AcmeTalkBundleServiceTalkRepository" /> <service id="acme.talk.service" class="AcmeTalkBundleServiceTalkService" scope="prototype"> <property name="talkRepository" type="service" id="acme.talk.repo" /> </service> </services> </container>
  53. 53. The state of DI in PHP
  54. 54. The state of DI in PHP Flow3 – Constructor Injection <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; /** * @FLOW3Scope("session") */ class StandardController extends TYPO3FLOW3MVCControllerActionController { /** * @var AcmeDemoServiceTalkServiceInterface */ protected $talkService; public function __construct( AcmeDemoServiceTalkService $talkService) { $this->talkService = $talkService; } public function indexAction() { } }
  55. 55. The state of DI in PHP Flow3 – Constructor Injection (manually) <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; /** * @FLOW3Scope("session") */ class StandardController extends TYPO3FLOW3MVCControllerActionController { /** * @var AcmeDemoServiceTalkServiceInterface */ protected $talkService; public function __construct( AcmeDemoServiceTalkService $talkService) { $this->talkService = $talkService; } public function indexAction() { } }
  56. 56. The state of DI in PHP Flow3 – Setter Injection (manually) <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; /** * @FLOW3Scope("session") */ class StandardController extends TYPO3FLOW3MVCControllerActionController { /** * @var AcmeDemoServiceTalkServiceInterface */ protected $talkService; public function setTalkService( AcmeDemoServiceTalkService $talkService) { $this->talkService = $talkService; } public function indexAction() { } }
  57. 57. The state of DI in PHP Flow3 – Setter Injection (manually) File Objects.yaml in Packages/Application/Acme.Demo/Configuration # @package Acme AcmeDemoControllerStandardController: properties: talkService: object: AcmeDemoServiceTalkService
  58. 58. The state of DI in PHP Flow3 – Setter Injection (Automagic) <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; /** * @FLOW3Scope("session") */ class StandardController extends TYPO3FLOW3MVCControllerActionController { /** * @var AcmeDemoServiceTalkServiceInterface */ protected $talkService; public function injectTalkService( AcmeDemoServiceTalkService $talkService) { $this->talkService = $talkService; } public function indexAction() { } }
  59. 59. The state of DI in PHP Flow3 – Setter Injection (Automagic) <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; /** * @FLOW3Scope("session") */ class StandardController extends TYPO3FLOW3MVCControllerActionController { /** * @var AcmeDemoServiceTalkServiceInterface */ protected $talkService; public function injectSomethingElse( AcmeDemoServiceTalkService $talkService) { $this->talkService = $talkService; } public function indexAction() { } }
  60. 60. The state of DI in PHP Flow3 – Property Injection <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; /** * @FLOW3Scope("session") */ class StandardController extends TYPO3FLOW3MVCControllerActionController { /** * @var AcmeDemoServiceTalkService * @FLOW3Inject */ protected $talkService; public function indexAction() { } }
  61. 61. The state of DI in PHP Flow3 – Property Injection (with Interface) <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; /** * @FLOW3Scope("session") */ class StandardController extends TYPO3FLOW3MVCControllerActionController { /** * @var AcmeDemoServiceTalkServiceInterface * @FLOW3Inject */ protected $talkService; public function indexAction() { } }
  62. 62. The state of DI in PHP Flow3 – Property Injection (with Interface) File Objects.yaml in Packages/Application/Acme.Demo/Configuration # @package Acme AcmeDemoServiceTalkServiceInterface: className: AcmeDemoServiceTalkService
  63. 63. The state of DI in PHP Flow3 – Scoping <?php namespace AcmeDemoController; use TYPO3FLOW3Annotations as FLOW3; /** * @FLOW3Scope("session") */ class StandardController extends TYPO3FLOW3MVCControllerActionController { /** * @var AcmeDemoServiceTalkServiceInterface * @FLOW3Inject */ protected $talkService; public function indexAction() { } }
  64. 64. The state of DI in PHP The future?
  65. 65. The state of DI in PHP The future (or what`s missing)?
  66. 66. The state of DI in PHP PSR for DI container missing!
  67. 67. The state of DI in PHP IDE support is missing...
  68. 68. Thank you!
  69. 69. http://joind.in/4768
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×