real world dependency injection se - phpugrhh
DESCRIPTION
TRANSCRIPT
![Page 1: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/1.jpg)
Real World Dependency Injection
Special Edition :)
![Page 2: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/2.jpg)
Real World Dependency Injection – Special Edition
Über mich
Stephan Hochdörfer, bitExpert AG
Department Manager Research Labs
PHP`ler seit 1999
@shochdoerfer
![Page 3: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/3.jpg)
Real World Dependency Injection – Special Edition
Was sind Dependencies?
![Page 4: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/4.jpg)
Sind Dependencies schlecht?
Real World Dependency Injection – Special Edition
![Page 5: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/5.jpg)
Sind Dependencies schlecht?
Normalerweise nicht...
Real World Dependency Injection – Special Edition
![Page 6: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/6.jpg)
Sind Dependencies schlecht?
…so lange Dependencies nicht hartcodiert sind!
Real World Dependency Injection – Special Edition
![Page 7: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/7.jpg)
Starke Kopplung
Real World Dependency Injection – Special Edition
![Page 8: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/8.jpg)
Keine Wiederverwendung!
Real World Dependency Injection – Special Edition
![Page 9: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/9.jpg)
Keine Isolation, nicht testbar!
Real World Dependency Injection – Special Edition
![Page 10: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/10.jpg)
Real World Dependency Injection – Special Edition
Dependency Wahnsinn!
![Page 11: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/11.jpg)
s
Real World Dependency Injection – Special Edition
„new“ is evil!
![Page 12: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/12.jpg)
<?phpclass DeletePage extends Mvc_Action_AAction {
private $pageManager;
public function __construct() {$this->pageManager = new PageManager();
}
protected function execute(Mvc_Request $request) {$this->pageManager->delete(
(int) $request->get('pageId'));
}}
Real World Dependency Injection – Special Edition
„new“ is evil!
![Page 13: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/13.jpg)
Real World Dependency Injection – Special Edition
„new“ is evil!
<?phpclass DeletePage extends Mvc_Action_AAction {
private $pageManager;
public function __construct(PageManager $pm) {$this->pageManager = $pm;
}
protected function execute(Mvc_Request $request) {$this->pageManager->delete(
(int) $request->get('pageId'));
}}
![Page 14: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/14.jpg)
"High-level modules should not depend on low-level modules.
Both should depend on abstractions."
Robert C. Martin
Real World Dependency Injection – Special Edition
![Page 15: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/15.jpg)
Interfaces als Vertrag
Real World Dependency Injection – Special Edition
![Page 16: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/16.jpg)
Real World Dependency Injection – Special Edition
„new“ is evil!
<?phpclass DeletePage extends Mvc_Action_AAction {
private $pageManager;
public function __construct(IPageManager $pm) {$this->pageManager = $pm;
}
protected function execute(Mvc_Request $request) {$this->pageManager->delete(
(int) $request->get('pageId'));
}}
![Page 17: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/17.jpg)
Wie verwaltet man Dependencies?
Real World Dependency Injection – Special Edition
![Page 18: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/18.jpg)
Händisch? Mit der heißen Nadel gestrickt...
Real World Dependency Injection – Special Edition
![Page 19: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/19.jpg)
Automatismus notwendig!
Einfacher Container vs. Fullstack DI Framework
Real World Dependency Injection – Special Edition
![Page 20: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/20.jpg)
Was ist Dependency Injection?
Consumer
Real World Dependency Injection – Special Edition
![Page 21: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/21.jpg)
Was ist Dependency Injection?
Consumer Dependencies
Real World Dependency Injection – Special Edition
![Page 22: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/22.jpg)
Was ist Dependency Injection?
Consumer Dependencies Container
Real World Dependency Injection – Special Edition
![Page 23: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/23.jpg)
Was ist Dependency Injection?
Consumer Dependencies Container
Real World Dependency Injection – Special Edition
![Page 24: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/24.jpg)
Wie Dependencies injizieren?
Real World Dependency Injection – Special Edition
![Page 25: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/25.jpg)
Constructor Injection
<?php
class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $sampleDao;
public function __construct(ISampleDao $sampleDao) {$this->sampleDao = $sampleDao;
}}
Real World Dependency Injection – Special Edition
![Page 26: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/26.jpg)
Setter Injection<?php
class MySampleService implements IMySampleService { /** * @var ISampleDao */ private $sampleDao;
public function setSampleDao(ISampleDao $sampleDao){$this->sampleDao = $sampleDao;
}}
Real World Dependency Injection – Special Edition
![Page 27: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/27.jpg)
Interface Injection<?php
interface IApplicationContextAware { public function setCtx(IApplicationContext $ctx);}
Real World Dependency Injection – Special Edition
![Page 28: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/28.jpg)
Interface Injection<?php
class MySampleService implements IMySampleService,IApplicationContextAware { /** * @var IApplicationContext */ private $ctx;
public function setCtx(IApplicationContext $ctx) {$this->ctx = $ctx;
}}
Real World Dependency Injection – Special Edition
![Page 29: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/29.jpg)
Real World Dependency Injection – Special Edition
Wie verbinden wir nun alles?
![Page 30: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/30.jpg)
Real World Dependency Injection – Special Edition
Annotations<?php
class MySampleService implements IMySampleService { private $sampleDao;
/** * @Inject */ public function __construct(ISampleDao $sampleDao) {
$this->sampleDao = $sampleDao; }}
![Page 31: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/31.jpg)
Real World Dependency Injection – Special Edition
Annotations<?php
class MySampleService implements IMySampleService { private $sampleDao;
/** * @Inject * @Named('TheSampleDao') */ public function __construct(ISampleDao $sampleDao) {
$this->sampleDao = $sampleDao; }}
![Page 32: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/32.jpg)
Externe Konfiguration - XML
<?xml version="1.0" encoding="UTF-8" ?><beans>
<bean id="SampleDao" class="SampleDao"><constructor-arg value="app_sample" /><constructor-arg value="iSampleId" /><constructor-arg value="BoSample" />
</bean>
<bean id="SampleService" class="MySampleService"><constructor-arg ref="SampleDao" />
</bean></beans>
Real World Dependency Injection – Special Edition
![Page 33: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/33.jpg)
Externe Konfiguration - YAML
services: SampleDao: class: SampleDao arguments: ['app_sample', 'iSampleId', 'BoSample'] SampleService: class: SampleService arguments: [@SampleDao]
Real World Dependency Injection – Special Edition
![Page 34: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/34.jpg)
Externe Konfiguration - PHP<?phpclass BeanCache extends Beanfactory_Container_PHP {
protected function createSampleDao() {$oBean = new SampleDao('app_sample',
'iSampleId', 'BoSample');return $oBean;
}
protected function createMySampleService() {$oBean = new MySampleService(
$this->getBean('SampleDao'));return $oBean;
}}
Real World Dependency Injection – Special Edition
![Page 35: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/35.jpg)
Real World Dependency Injection – Special Edition
Beispiele gefällig?
![Page 36: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/36.jpg)
Unittesting einfach gemacht
Real World Dependency Injection – Special Edition
![Page 37: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/37.jpg)
Unittesting einfach gemacht<?phprequire_once 'PHPUnit/Framework.php';
class ServiceTest extends PHPUnit_Framework_TestCase { public function testSampleService() {
// set up dependencies $sampleDao = $this->getMock('ISampleDao'); $service = new MySampleService($sampleDao);
// run test case$return = $service->doWork();
// check assertions$this->assertTrue($return);
}}
Real World Dependency Injection – Special Edition
![Page 38: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/38.jpg)
Eine Klasse, mehrfache Ausprägung
Real World Dependency Injection – Special Edition
![Page 39: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/39.jpg)
Eine Klasse, mehrfache Ausprägung
Page ExporterPage Exporter
Real World Dependency Injection – Special Edition
![Page 40: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/40.jpg)
Eine Klasse, mehrfache Ausprägung
Page ExporterPage Exporter
Released / PublishedPages
Released / PublishedPages
WorkingcopyPages
WorkingcopyPages
Real World Dependency Injection – Special Edition
![Page 41: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/41.jpg)
Eine Klasse, mehrfache Ausprägung<?phpabstract class PageExporter { protected function setPageDao(IPageDao $pageDao) { $this->pageDao = $pageDao; }}
Real World Dependency Injection – Special Edition
![Page 42: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/42.jpg)
Eine Klasse, mehrfache Ausprägung<?phpabstract class PageExporter { protected function setPageDao(IPageDao $pageDao) { $this->pageDao = $pageDao; }}
Zur Erinnerung: Der Vertrag!
Real World Dependency Injection – Special Edition
![Page 43: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/43.jpg)
Eine Klasse, mehrfache Ausprägung
<?phpclass PublishedPageExporter extends PageExporter { public function __construct() { $this->setPageDao(new PublishedPageDao()); }}
class WorkingCopyPageExporter extends PageExporter { public function __construct() { $this->setPageDao(new WorkingCopyPageDao()); }}
Real World Dependency Injection – Special Edition
![Page 44: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/44.jpg)
"Only deleted code is good code!"Oliver Gierke
Eine Klasse, mehrfache Ausprägung
Real World Dependency Injection – Special Edition
![Page 45: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/45.jpg)
Eine Klasse, mehrfache Ausprägung<?phpclass PageExporter { public function __construct(IPageDao $pageDao) { $this->pageDao = $pageDao; }}
Real World Dependency Injection – Special Edition
![Page 46: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/46.jpg)
Eine Klasse, mehrfache Ausprägung<?xml version="1.0" encoding="UTF-8" ?><beans>
<bean id="ExportLive" class="PageExporter"><constructor-arg ref="PublishedPageDao" />
</bean>
<bean id="ExportWorking" class="PageExporter"><constructor-arg ref="WorkingCopyPageDao" />
</bean></beans>
Real World Dependency Injection – Special Edition
![Page 47: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/47.jpg)
Eine Klasse, mehrfache Ausprägung
<?php
// create ApplicationContext instance$ctx = new ApplicationContext();
// retrieve live exporter$exporter = $ctx->getBean('ExportLive');
// retrieve working copy exporter$exporter = $ctx->getBean('ExportWorking');
Real World Dependency Injection – Special Edition
![Page 48: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/48.jpg)
Eine Klasse, mehrfache Ausprägung II
Real World Dependency Injection – Special Edition
![Page 49: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/49.jpg)
Eine Klasse, mehrfache Ausprägung II
http://editor.loc/page/[id]/headline/
http://editor.loc/page/[id]/content/
http://editor.loc/page/[id]/teaser/
Real World Dependency Injection – Special Edition
![Page 50: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/50.jpg)
<?phpclass EditPart extends Mvc_Action_AFormAction {
private $pagePartsManager;private $type;
public function __construct(IPagePartsManager $pm) {$this->pagePartsManager = $pm;
}
public function setType($ptype) {$this->type = (int) $type;
}
protected function process(Bo_ABo $formBackObj) {}
}
Eine Klasse, mehrfache Ausprägung II
Real World Dependency Injection – Special Edition
![Page 51: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/51.jpg)
Eine Klasse, mehrfache Ausprägung II<?xml version="1.0" encoding="UTF-8" ?><beans>
<bean id="EditHeadline" class="EditPart"><constructor-arg ref="PagePartDao" /><property name="Type" const="PType::Headline" />
</bean>
<bean id="EditContent" class="EditPart"><constructor-arg ref="PagePartDao" /><property name="Type" const="PType::Content" />
</bean>
</beans>
Real World Dependency Injection – Special Edition
![Page 52: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/52.jpg)
Externe Services mocken
Real World Dependency Injection – Special Edition
![Page 53: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/53.jpg)
Externe Services mocken
BookingmanagerBookingmanager WS-Connector
WS-Connector WebserviceWebservice
Real World Dependency Injection – Special Edition
![Page 54: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/54.jpg)
Externe Services mocken
BookingmanagerBookingmanager WS-Connector
WS-Connector WebserviceWebservice
Real World Dependency Injection – Special Edition
Zur Erinnerung: Der Vertrag!
![Page 55: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/55.jpg)
Externe Services mocken
BookingmanagerBookingmanager FS-Connector
FS-Connector FilesystemFilesystem
Real World Dependency Injection – Special Edition
![Page 56: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/56.jpg)
Externe Services mocken
BookingmanagerBookingmanager FS-Connector
FS-Connector FilesystemFilesystem
erfüllt denVertrag!
Real World Dependency Injection – Special Edition
![Page 57: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/57.jpg)
Sauberer, lesbarer Code
Real World Dependency Injection – Special Edition
![Page 58: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/58.jpg)
Sauberer, lesbarer Code<?phpclass DeletePage extends Mvc_Action_AAction {
private $pageManager;
public function __construct(IPageManager $pm) {$this->pageManager = $pm;
}
protected function execute(Mvc_Request $request) {$this->pageManager->delete(
(int) $request->get('pageId'));
return new ModelAndView($this->getSuccessView());}
}
Real World Dependency Injection – Special Edition
![Page 59: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/59.jpg)
Keine Framework Abhängigkeit
Real World Dependency Injection – Special Edition
![Page 60: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/60.jpg)
Keine Framework Abhängigkeit<?phpclass MySampleService implements IMySampleService { private $sampleDao;
public function __construct(ISampleDao $sampleDao) {$this->sampleDao = $sampleDao;
}
public function getSample($sampleId) {try { return $this->sampleDao->readById($sampleId);}catch(DaoException $exception) {}
}}
Real World Dependency Injection – Special Edition
![Page 61: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/61.jpg)
Real World Dependency Injection – Special Edition
Wie sieht das nun in der Praxis aus?
![Page 62: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/62.jpg)
Real World Dependency Injection – Special Edition
![Page 63: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/63.jpg)
Zend\Di – Erste Schritte
Real World Dependency Injection – Special Edition
<?phpnamespace Acme;
class TalkService {public function __construct() {}
public function getTalks() {}
}
![Page 64: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/64.jpg)
Zend\Di – Erste Schritte
Real World Dependency Injection – Special Edition
<?php
$di = new \Zend\Di\Di();
$service = $di->get('Acme\TalkService');$service->getTalks();
![Page 65: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/65.jpg)
Zend\Di – Constructor Injection
Real World Dependency Injection – Special Edition
<?phpnamespace Acme;
interface GenericRepository {public function readTalks();
}
class TalkRepository implements GenericRepository {public function readTalks() {}
}
class TalkService {public function __construct(TalkRepository $repo) {}
public function getTalks() {}
}
![Page 66: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/66.jpg)
Zend\Di – Constructor Injection
Real World Dependency Injection – Special Edition
<?php
$di = new \Zend\Di\Di();
$service = $di->get('Acme\TalkService');$service->getTalks();
![Page 67: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/67.jpg)
Zend\Di – Setter Injection
Real World Dependency Injection – Special Edition
<?phpnamespace Acme;
class Logger {public function doLog($logMsg) {}
}
class TalkService {public function __construct(TalkRepository $repo) {}
public function setLogger(Logger $logger) {}
public function getTalks() {}
}
![Page 68: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/68.jpg)
Zend\Di – Setter Injection
Real World Dependency Injection – Special Edition
<?php$di = new \Zend\Di\Di();$di->configure(
new Zend\Di\Configuration(array(
'definition' => array('class' => array(
'Acme\TalkService' => array('setLogger' => array('required' => true)
))
))
));
$service = $di->get('Acme\TalkService');var_dump($service);
![Page 69: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/69.jpg)
Zend\Di – Interface Injection
Real World Dependency Injection – Special Edition
<?phpnamespace 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() {}
}
![Page 70: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/70.jpg)
Zend\Di – Interface Injection
Real World Dependency Injection – Special Edition
<?php
$di = new \Zend\Di\Di();
$service = $di->get('Acme\TalkService');$service->getTalks();
![Page 71: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/71.jpg)
Zend\Di – Grundsätzliches
Real World Dependency Injection – Special Edition
<?php
$di = new \Zend\Di\Di();
$service = $di->get('Acme\TalkService');var_dump($service);
$service2 = $di->get('Acme\TalkService');var_dump($service2); // gleiche Instanz wie $service
$service3 = $di->get('Acme\TalkService',array(
'repo' => new \phpbnl12\TalkRepository())
);var_dump($service3); // neue Instanz
![Page 72: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/72.jpg)
Zend\Di – Builder Definition
Real World Dependency Injection – Special Edition
<?php// Dependency beschreiben$dep = new \Zend\Di\Definition\Builder\PhpClass();$dep->setName('Acme\TalkRepository');
// Klasse beschreiben$class = new \Zend\Di\Definition\Builder\PhpClass();$class->setName('Acme\TalkService');
// Injection Methode beschreiben$im = new \Zend\Di\Definition\Builder\InjectionMethod();$im->setName('__construct');$im->addParameter('repo', 'Acme\TalkRepository');$class->addInjectionMethod($im);
// Builder konfigurieren$builder = new \Zend\Di\Definition\BuilderDefinition();$builder->addClass($dep);$builder->addClass($class);
![Page 73: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/73.jpg)
Zend\Di – Builder Definition
Real World Dependency Injection – Special Edition
<?php
// zur DefinitionList hinzufügen$defList = new \Zend\Di\DefinitionList($builder);$di = new \Zend\Di\Di($defList);
$service = $di->get('Acme\TalkService');var_dump($service);
![Page 74: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/74.jpg)
Real World Dependency Injection – Special Edition
![Page 75: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/75.jpg)
Symfony2
Real World Dependency Injection – Special Edition
<?phpnamespace Acme\TalkBundle\Controller;use Symfony\Bundle\FrameworkBundle\Controller\Controller;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
class TalkController extends Controller { /** * @Route("/", name="_talk") * @Template() */ public function indexAction() { $service = $this->get('acme.talk.service'); return array(); }}
![Page 76: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/76.jpg)
Symfony2 – Konfigurationsdatei
Real World Dependency Injection – Special Edition
<?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/serviceshttp://symfony.com/schema/dic/services/services-1.0.xsd">
</container>
Datei services.xml in src/Acme/DemoBundle/Resources/config
![Page 77: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/77.jpg)
Symfony2 – Constructor Injection
Real World Dependency Injection – Special Edition
<?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="Acme\TalkBundle\Service\TalkRepository" />
<service id="acme.talk.service" class="Acme\TalkBundle\Service\TalkService">
<argument type="service" id="acme.talk.repo" /></service>
</services></container>
![Page 78: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/78.jpg)
Symfony2 – Setter Injection
Real World Dependency Injection – Special Edition
<?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="Acme\TalkBundle\Service\Logger" />
<service id="acme.talk.repo" class="Acme\TalkBundle\Service\TalkRepository" />
<service id="acme.talk.service" class="Acme\TalkBundle\Service\TalkService">
<argument type="service" id="acme.talk.repo" /><call method="setLogger">
<argument type="service" id="acme.talk.logger" /></call>
</service></services>
</container>
![Page 79: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/79.jpg)
Symfony2 – Setter Injection (optional)
Real World Dependency Injection – Special Edition
<?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="Acme\TalkBundle\Service\Logger" />
<service id="acme.talk.repo" class="Acme\TalkBundle\Service\TalkRepository" />
<service id="acme.talk.service" class="Acme\TalkBundle\Service\TalkService">
<argument type="service" id="acme.talk.repo" /><call method="setLogger">
<argument type="service" id="acme.talk.logger" on-invalid="ignore" />
</call></service>
</services></container>
![Page 80: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/80.jpg)
Symfony2 – Property Injection
Real World Dependency Injection – Special Edition
<?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="Acme\TalkBundle\Service\TalkRepository" />
<service id="acme.talk.service" class="Acme\TalkBundle\Service\TalkService">
<property name="talkRepository" type="service" id="acme.talk.repo" />
</service></services>
</container>
![Page 81: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/81.jpg)
Symfony2 – private / öffentliche Services
Real World Dependency Injection – Special Edition
<?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="Acme\TalkBundle\Service\Logger" public="false" />
<service id="acme.talk.repo" class="Acme\TalkBundle\Service\TalkRepository" />
<service id="acme.talk.service" class="Acme\TalkBundle\Service\TalkService">
<argument type="service" id="acme.talk.repo" /><call method="setLogger">
<argument type="service" id="acme.talk.logger" /></call>
</service></services>
</container>
![Page 82: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/82.jpg)
Symfony2 – Vererbung
Real World Dependency Injection – Special Edition
<?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="Acme\TalkBundle\Service\TalkService" 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>
![Page 83: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/83.jpg)
Symfony2 – Scoping
Real World Dependency Injection – Special Edition
<?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="Acme\TalkBundle\Service\TalkRepository" />
<service id="acme.talk.service"class="Acme\TalkBundle\Service\TalkService" scope="prototype"><property name="talkRepository" type="service"
id="acme.talk.repo" /></service>
</services></container>
![Page 84: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/84.jpg)
Real World Dependency Injection – Special Edition
![Page 85: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/85.jpg)
Flow3 – Constructor Injection
Real World Dependency Injection – Special Edition
<?phpnamespace Acme\Demo\Controller;
use TYPO3\FLOW3\Annotations as FLOW3;
/** * @FLOW3\Scope("session") */class StandardController extends \TYPO3\FLOW3\MVC\Controller\ActionController {
/** * @var \Acme\Demo\Service\TalkServiceInterface */protected $talkService;
public function __construct(\Acme\Demo\Service\TalkService $talkService) {$this->talkService = $talkService;
}
public function indexAction() {}
}
![Page 86: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/86.jpg)
Flow3 – Setter Injection (manuell)
Real World Dependency Injection – Special Edition
<?phpnamespace Acme\Demo\Controller;
use TYPO3\FLOW3\Annotations as FLOW3;
/** * @FLOW3\Scope("session") */class StandardController extends \TYPO3\FLOW3\MVC\Controller\ActionController {
/** * @var \Acme\Demo\Service\TalkServiceInterface */protected $talkService;
public function setTalkService(\Acme\Demo\Service\TalkService $talkService) {$this->talkService = $talkService;
}
public function indexAction() {}
}
![Page 87: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/87.jpg)
Flow3 – Setter Injection (manuell)
Real World Dependency Injection – Special Edition
Datei Objects.yaml in Packages/Application/Acme.Demo/Configuration# @package AcmeAcme\Demo\Controller\StandardController: properties: talkService: object: Acme\Demo\Service\TalkService
![Page 88: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/88.jpg)
Flow3 – Setter Injection (automatisch)
Real World Dependency Injection – Special Edition
<?phpnamespace Acme\Demo\Controller;
use TYPO3\FLOW3\Annotations as FLOW3;
/** * @FLOW3\Scope("session") */class StandardController extends \TYPO3\FLOW3\MVC\Controller\ActionController {
/** * @var \Acme\Demo\Service\TalkServiceInterface */protected $talkService;
public function injectTalkService(\Acme\Demo\Service\TalkService $talkService) {$this->talkService = $talkService;
}
public function indexAction() {}
}
![Page 89: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/89.jpg)
Flow3 – Setter Injection (automatisch)
Real World Dependency Injection – Special Edition
<?phpnamespace Acme\Demo\Controller;
use TYPO3\FLOW3\Annotations as FLOW3;
/** * @FLOW3\Scope("session") */class StandardController extends \TYPO3\FLOW3\MVC\Controller\ActionController {
/** * @var \Acme\Demo\Service\TalkServiceInterface */protected $talkService;
public function injectSomethingElse(\Acme\Demo\Service\TalkService $talkService) {$this->talkService = $talkService;
}
public function indexAction() {}
}
![Page 90: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/90.jpg)
Flow3 – Property Injection
Real World Dependency Injection – Special Edition
<?phpnamespace Acme\Demo\Controller;
use TYPO3\FLOW3\Annotations as FLOW3;
/** * @FLOW3\Scope("session") */class StandardController extends \TYPO3\FLOW3\MVC\Controller\ActionController {
/** * @var \Acme\Demo\Service\TalkService * @FLOW3\Inject */protected $talkService;
public function indexAction() {}
}
![Page 91: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/91.jpg)
Flow3 – Property Injection (mittels Interface)
Real World Dependency Injection – Special Edition
<?phpnamespace Acme\Demo\Controller;
use TYPO3\FLOW3\Annotations as FLOW3;
/** * @FLOW3\Scope("session") */class StandardController extends \TYPO3\FLOW3\MVC\Controller\ActionController {
/** * @var \Acme\Demo\Service\TalkServiceInterface * @FLOW3\Inject */protected $talkService;
public function indexAction() {}
}
![Page 92: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/92.jpg)
Flow3 – Property Injection (mittels Interface)
Real World Dependency Injection – Special Edition
# @package AcmeAcme\Demo\Service\TalkServiceInterface: className: 'Acme\Demo\Service\TalkService'
Datei Objects.yaml in Packages/Application/Acme.Demo/Configuration
![Page 93: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/93.jpg)
Flow3 – Scoping
Real World Dependency Injection – Special Edition
<?phpnamespace Acme\Demo\Controller;
use TYPO3\FLOW3\Annotations as FLOW3;
/** * @FLOW3\Scope("session") */class StandardController extends \TYPO3\FLOW3\MVC\Controller\ActionController {
/** * @var \Acme\Demo\Service\TalkServiceInterface * @FLOW3\Inject */protected $talkService;
public function indexAction() {}
}
![Page 94: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/94.jpg)
Vorteile
Real World Dependency Injection – Special Edition
![Page 95: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/95.jpg)
Vorteile
Lose Kopplung, gesteigerte Wiederverwendbarkeit !
Real World Dependency Injection – Special Edition
![Page 96: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/96.jpg)
Vorteile
Codeumfang reduzieren,Fokus auf das Wesentliche!
Real World Dependency Injection – Special Edition
![Page 97: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/97.jpg)
Vorteile
Hilft Entwicklern den Code besser zu verstehen!
Real World Dependency Injection – Special Edition
![Page 98: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/98.jpg)
Nachteil – Kein JSR330 für PHP
Bucket, Crafty, FLOW3, Imind_Context, PicoContainer, Pimple, Phemto,
Stubbles, Symfony 2.0, Sphicy, Solar, Substrate, XJConf, Yadif, Zend\Di , Lion Framework, Spiral Framework, Xyster
Framework, …
Real World Dependency Injection – Special Edition
![Page 99: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/99.jpg)
Nachteil – Entwickler müssen umdenken
Konfiguration ↔ Laufzeit
Real World Dependency Injection – Special Edition
![Page 100: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/100.jpg)
Real World Dependency Injection – Special Edition
Nachteil – Keine IDE Unterstützung!
![Page 101: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/101.jpg)
Vielen Dank!
![Page 102: Real World Dependency Injection SE - phpugrhh](https://reader033.vdocuments.mx/reader033/viewer/2022051412/54b4fd584a795945748b46a1/html5/thumbnails/102.jpg)
Image Creditshttp://www.sxc.hu/photo/1028452