SlideShare a Scribd company logo
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 1999
  7 years of DI experience (in PHP)
  S.Hochdoerfer@bitExpert.de
  @shochdoerfer
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“
                         Martin Fowler
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 and 5.3“
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() {
     }

     public function getTalks() {
     }
 }
The state of DI in PHP

 ZendDi – First steps
 <?php

 $di = new ZendDiDi();

 $service = $di->get('AcmeTalkService');
 $service->getTalks();
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() {
     }
 }
The state of DI in PHP

 ZendDi – Constructor Injection
 <?php

 $di = new ZendDiDi();

 $service = $di->get('AcmeTalkService');
 $service->getTalks();
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() {
     }
 }
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);
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() {
     }
 }
The state of DI in PHP

 ZendDi – Interface Injection
 <?php

 $di = new ZendDiDi();

 $service = $di->get('AcmeTalkService');
 $service->getTalks();
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
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);
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);
The state of DI in PHP
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();
     }
 }
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>
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>
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>
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>
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>
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>
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>
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>
The state of DI in PHP
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() {
     }
 }
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() {
     }
 }
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() {
     }
 }
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
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() {
     }
 }
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() {
     }
 }
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() {
     }
 }
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() {
     }
 }
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'
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() {
     }
 }
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

More Related Content

PDF
The state of DI - DPC12
PPT
PHPBootcamp - Zend Framework
PDF
Cli the other SAPI confoo11
PDF
Cli the other sapi pbc11
PDF
Zend Certification PHP 5 Sample Questions
PDF
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
PDF
Drupal 8's Multilingual APIs: Building for the Entire World
PDF
Practice exam php
The state of DI - DPC12
PHPBootcamp - Zend Framework
Cli the other SAPI confoo11
Cli the other sapi pbc11
Zend Certification PHP 5 Sample Questions
IPC2010SE Doctrine2 Enterprise Persistence Layer for PHP
Drupal 8's Multilingual APIs: Building for the Entire World
Practice exam php

What's hot (20)

PDF
CLI, the other SAPI phpnw11
PPTX
HTTP Middlewares in PHP by Eugene Dounar
PDF
Drupal 8's Multilingual APIs: Building for the Entire World
PDF
Nette framework (WebElement #28)
PPT
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)
PDF
The Beauty And The Beast Php N W09
PPT
Introduction to php php++
PDF
Melhorando sua API com DSLs
PPTX
PHP7 Presentation
PDF
Champaign-Urbana Javascript Meetup Talk (Jan 2020)
PPTX
Php.ppt
KEY
Zend Framework Study@Tokyo #2
PPTX
PDF
PHP7 is coming
PDF
fastcgi_conf and mime_types
PPTX
Php7 HHVM and co
DOCX
php questions
PDF
MidwestPHP Symfony2 Internals
PPT
Php mysql
PDF
Web services tutorial
CLI, the other SAPI phpnw11
HTTP Middlewares in PHP by Eugene Dounar
Drupal 8's Multilingual APIs: Building for the Entire World
Nette framework (WebElement #28)
ZFConf 2012: Dependency Management в PHP и Zend Framework 2 (Кирилл Чебунин)
The Beauty And The Beast Php N W09
Introduction to php php++
Melhorando sua API com DSLs
PHP7 Presentation
Champaign-Urbana Javascript Meetup Talk (Jan 2020)
Php.ppt
Zend Framework Study@Tokyo #2
PHP7 is coming
fastcgi_conf and mime_types
Php7 HHVM and co
php questions
MidwestPHP Symfony2 Internals
Php mysql
Web services tutorial
Ad

Similar to The state of DI in PHP - phpbnl12 (20)

PDF
What's New In Laravel 5
PDF
Living With Legacy Code
PDF
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
PPT
Zend Framework
PPS
Simplify your professional web development with symfony
PDF
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
PDF
Tips
PDF
Osiąganie mądrej architektury z Symfony2
PDF
Symfony2 revealed
PDF
Hibernate Presentation
PDF
Build powerfull and smart web applications with Symfony2
PDF
関西PHP勉強会 php5.4つまみぐい
PDF
Dependency injection in Drupal 8
ODP
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
PDF
Hexagonal architecture in PHP
PDF
ElePHPant7 - Introduction to PHP7
PDF
Current state-of-php
PDF
PHP Frameworks: I want to break free (IPC Berlin 2024)
PPTX
PHP 7 - A look at the future
PPT
Php mysql training-in-mumbai
What's New In Laravel 5
Living With Legacy Code
Domain-driven Design in PHP and Symfony - Drupal Camp Wroclaw!
Zend Framework
Simplify your professional web development with symfony
Kicking off with Zend Expressive and Doctrine ORM (PHP Srbija 2017)
Tips
Osiąganie mądrej architektury z Symfony2
Symfony2 revealed
Hibernate Presentation
Build powerfull and smart web applications with Symfony2
関西PHP勉強会 php5.4つまみぐい
Dependency injection in Drupal 8
Building Web Services with Zend Framework (PHP Benelux meeting 20100713 Vliss...
Hexagonal architecture in PHP
ElePHPant7 - Introduction to PHP7
Current state-of-php
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP 7 - A look at the future
Php mysql training-in-mumbai
Ad

More from Stephan Hochdörfer (20)

PDF
Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...
PDF
Phing for power users - frOSCon8
PDF
Offline strategies for HTML5 web applications - frOSCon8
PDF
Offline Strategies for HTML5 Web Applications - oscon13
PDF
Real World Dependency Injection - oscon13
PDF
Dependency Injection in PHP - dwx13
PDF
Offline Strategien für HTML5 Web Applikationen - dwx13
PDF
Your Business. Your Language. Your Code - dpc13
PDF
Phing for power users - dpc_uncon13
PDF
Offline Strategies for HTML5 Web Applications - ipc13
PDF
Offline-Strategien für HTML5 Web Applikationen - wmka
PDF
Offline-Strategien für HTML5 Web Applikationen - bedcon13
PDF
Real World Dependency Injection - phpugffm13
PDF
Testing untestable code - ConFoo13
PDF
A Phing fairy tale - ConFoo13
PDF
Offline strategies for HTML5 web applications - ConFoo13
PDF
Offline-Strategien für HTML5Web Applikationen - WMMRN12
PDF
Testing untestable code - IPC12
PDF
Offline strategies for HTML5 web applications - IPC12
PDF
Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12
Offline. Na und? Strategien für offlinefähige Applikationen in HTML5 - Herbst...
Phing for power users - frOSCon8
Offline strategies for HTML5 web applications - frOSCon8
Offline Strategies for HTML5 Web Applications - oscon13
Real World Dependency Injection - oscon13
Dependency Injection in PHP - dwx13
Offline Strategien für HTML5 Web Applikationen - dwx13
Your Business. Your Language. Your Code - dpc13
Phing for power users - dpc_uncon13
Offline Strategies for HTML5 Web Applications - ipc13
Offline-Strategien für HTML5 Web Applikationen - wmka
Offline-Strategien für HTML5 Web Applikationen - bedcon13
Real World Dependency Injection - phpugffm13
Testing untestable code - ConFoo13
A Phing fairy tale - ConFoo13
Offline strategies for HTML5 web applications - ConFoo13
Offline-Strategien für HTML5Web Applikationen - WMMRN12
Testing untestable code - IPC12
Offline strategies for HTML5 web applications - IPC12
Große Systeme, lose Kopplung, Spaß bei der Arbeit! - WDC12

Recently uploaded (20)

PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
MYSQL Presentation for SQL database connectivity
PDF
KodekX | Application Modernization Development
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Approach and Philosophy of On baking technology
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Big Data Technologies - Introduction.pptx
PPTX
sap open course for s4hana steps from ECC to s4
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Mobile App Security Testing_ A Comprehensive Guide.pdf
MYSQL Presentation for SQL database connectivity
KodekX | Application Modernization Development
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
Building Integrated photovoltaic BIPV_UPV.pdf
Review of recent advances in non-invasive hemoglobin estimation
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Approach and Philosophy of On baking technology
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Spectral efficient network and resource selection model in 5G networks
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Understanding_Digital_Forensics_Presentation.pptx
Big Data Technologies - Introduction.pptx
sap open course for s4hana steps from ECC to s4
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
20250228 LYD VKU AI Blended-Learning.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

The state of DI in PHP - phpbnl12

  • 1. The state of DI in PHP
  • 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. The state of DI in PHP What`s this talk about?
  • 4. The state of DI in PHP What`s this talk about? No framework bashing! Hopefully :)
  • 5. The state of DI in PHP What`s this talk about? It`s all about how DI is implemented.
  • 6. The state of DI in PHP What`s DI about?
  • 7. The state of DI in PHP Inversion of control
  • 8. The state of DI in PHP „Hollywood principle“
  • 9. The state of DI in PHP What`s DI about? new TalkService(new TalkRepository());
  • 10. The state of DI in PHP What`s DI about? Consumer
  • 11. The state of DI in PHP What`s DI about? Consumer Dependencies
  • 12. The state of DI in PHP What`s DI about? Consumer Dependencies Container
  • 13. The state of DI in PHP What`s DI about? Consumer Dependencies Container
  • 14. The state of DI in PHP A little bit of history...
  • 15. The state of DI in PHP 1988 „Designing Reusable Classes“ Ralph E. Johnson & Brian Foote
  • 16. The state of DI in PHP 1996 „The Dependency Inversion Principle“ Robert C. Martin
  • 17. The state of DI in PHP 2002
  • 18. The state of DI in PHP 2003 Spring Framework 0.9 released
  • 19. The state of DI in PHP 2004 „Inversion of Control Containers and the Dependency Injection pattern“ Martin Fowler
  • 20. The state of DI in PHP 2004 Spring Framework 1.0 released
  • 21. The state of DI in PHP 2004 PHP 5.0 released
  • 22. The state of DI in PHP 2005 / 2006 Garden, a lightweight DI container for PHP.
  • 23. The state of DI in PHP 2006 First PHP5 framework with DI support
  • 24. The state of DI in PHP 2007 International PHP Conference 2007 features 2 talks about DI.
  • 25. The state of DI in PHP 2009 PHP 5.3.0 released
  • 26. The state of DI in PHP 2010 Fabien Potencier talks about „Dependency Injection in PHP 5.2 and 5.3“
  • 27. The state of DI in PHP 2011 Symfony2, Flow3, Zend Framework 2 (beta)
  • 28. The state of DI in PHP 2012 And now?
  • 29. The state of DI in PHP DI has finally arrived in the PHP world!
  • 30. The state of DI in PHP Let`s have a look at the different implementations!
  • 31. The state of DI in PHP
  • 32. The state of DI in PHP ZendDi – First steps <?php namespace Acme; class TalkService { public function __construct() { } public function getTalks() { } }
  • 33. The state of DI in PHP ZendDi – First steps <?php $di = new ZendDiDi(); $service = $di->get('AcmeTalkService'); $service->getTalks();
  • 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. The state of DI in PHP ZendDi – Constructor Injection <?php $di = new ZendDiDi(); $service = $di->get('AcmeTalkService'); $service->getTalks();
  • 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. 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. 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. The state of DI in PHP ZendDi – Interface Injection <?php $di = new ZendDiDi(); $service = $di->get('AcmeTalkService'); $service->getTalks();
  • 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. 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. 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. The state of DI in PHP
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. The state of DI in PHP
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. The state of DI in PHP The future?
  • 65. The state of DI in PHP The future (or what`s missing)?
  • 66. The state of DI in PHP PSR for DI container missing!
  • 67. The state of DI in PHP IDE support is missing...