the state of di - dpc12
DESCRIPTION
TRANSCRIPT
![Page 1: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/1.jpg)
The state of DI in PHP
![Page 2: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/2.jpg)
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)
@shochdoerfer
![Page 3: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/3.jpg)
The state of DI in PHP
What are Dependencies?
![Page 4: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/4.jpg)
Are Dependencies bad?
The state of DI in PHP
![Page 5: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/5.jpg)
Are Dependencies bad?
Not at all!
The state of DI in PHP
![Page 6: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/6.jpg)
Are Dependencies bad?
Hard-coded dependencies are bad!
The state of DI in PHP
![Page 7: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/7.jpg)
Tightly coupled code
The state of DI in PHP
![Page 8: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/8.jpg)
No reuse of components
The state of DI in PHP
![Page 9: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/9.jpg)
No isolation, not testable!
The state of DI in PHP
![Page 10: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/10.jpg)
The state of DI in PHP
Dependency madness!
![Page 11: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/11.jpg)
What`s DI about?
The state of DI in PHP
![Page 12: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/12.jpg)
What`s DI about?
The state of DI in PHP
new TalkService(new TalkRepository());
![Page 13: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/13.jpg)
What`s DI about?
Consumer
The state of DI in PHP
![Page 14: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/14.jpg)
What`s DI about?
Consumer Dependencies
The state of DI in PHP
![Page 15: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/15.jpg)
What`s DI about?
Consumer Dependencies Container
The state of DI in PHP
![Page 16: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/16.jpg)
What`s DI about?
Consumer Dependencies Container
The state of DI in PHP
![Page 17: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/17.jpg)
A little bit of history...
The state of DI in PHP
![Page 18: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/18.jpg)
1988
The state of DI in PHP
„Designing Reusable Classes“Ralph E. Johnson & Brian Foote
![Page 19: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/19.jpg)
1996
The state of DI in PHP
„The Dependency Inversion Principle“Robert C. Martin
![Page 20: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/20.jpg)
2004
The state of DI in PHP
„Inversion of Control Containers and the Dependency Injection pattern“
Martin Fowler
![Page 21: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/21.jpg)
2005 / 2006
The state of DI in PHP
Garden, a lightweight DI container for PHP.
![Page 22: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/22.jpg)
2006
The state of DI in PHP
First PHP5 framework with DI support
![Page 23: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/23.jpg)
2007
The state of DI in PHP
International PHP Conference 2007 features 2 talks about DI.
![Page 24: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/24.jpg)
2011
The state of DI in PHP
Zend Framework 2 (beta),Symfony2, Flow3, ...
![Page 25: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/25.jpg)
Choose wisely!
Simple Container vs. Full stacked DI Framework
The state of DI in PHP
![Page 26: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/26.jpg)
The state of DI in PHP
Pimple
![Page 27: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/27.jpg)
Pimple – First steps
The state of DI in PHP
<?php
class TalkService {public function __construct() {}
public function getTalks() {}
}
![Page 28: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/28.jpg)
Pimple – First steps
The state of DI in PHP
<?phprequire_once '/path/to/Pimple.php';require_once '/path/to/TalkService.php';
// create the Container$container = new Pimple();
// define talkService object in container$container['talkService'] = function ($c) { return new TalkService();};
// instantiate talkService from container$talkService = $container['talkService'];
![Page 29: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/29.jpg)
Pimple – Constructor Injection
The state of DI in PHP
<?php
interface GenericRepository {public function readTalks();
}
class TalkRepository implements GenericRepository {public function readTalks() {}
}
class TalkService {public function __construct(TalkRepository $repo) {}
public function getTalks() {}
}
![Page 30: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/30.jpg)
Pimple – Constructor Injection
The state of DI in PHP
<?phprequire_once '/path/to/Pimple.php';require_once '/path/to/TalkService.php';
// create the Container$container = new Pimple();
// define services in container$container['talkRepository'] = function ($c) { return new TalkRepository();};$container['talkService'] = function ($c) { return new TalkService($c['talkRepository']);};
// instantiate talkService from container$talkService = $container['talkService'];
![Page 31: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/31.jpg)
Pimple – Setter Injection
The state of DI in PHP
<?php
class Logger {public function doLog($logMsg) {}
}
class TalkService {public function __construct(TalkRepository $repo) {}
public function setLogger(Logger $logger) {}
public function getTalks() {}
}
![Page 32: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/32.jpg)
Pimple – Setter Injection
The state of DI in PHP
<?phprequire_once '/path/to/Pimple.php';require_once '/path/to/TalkService.php';
// create the Container$container = new Pimple();
// define services in container$container['logger'] = function ($c) { return new Logger();};$container['talkRepository'] = function ($c) { return new TalkRepository();};$container['talkService'] = function ($c) { $service = new TalkService($c['talkRepository']); $service->setLogger($c['logger']); return $service;};
// instantiate talkService from container$talkService = $container['talkService'];
![Page 33: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/33.jpg)
Pimple – General usage
The state of DI in PHP
<?phprequire_once '/path/to/Pimple.php';require_once '/path/to/TalkService.php';
// create the Container$container = new Pimple();
// define services in container$container['loggerShared'] = $c->share(function ($c) { return new Logger();)};$container['logger'] = function ($c) { return new Logger();};
// instantiate logger from container$logger = $container['logger'];
// instantiate shared logger from container (same instance!)$logger2 = $container['loggerShared'];$logger3 = $container['loggerShared'];
![Page 34: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/34.jpg)
The state of DI in PHP
Bucket
![Page 35: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/35.jpg)
Bucket – Constructor Injection
The state of DI in PHP
<?php
interface GenericRepository {public function readTalks();
}
class TalkRepository implements GenericRepository {public function readTalks() {}
}
class TalkService {public function __construct(TalkRepository $repo) {}
public function getTalks() {}
}
![Page 36: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/36.jpg)
Bucket – Constructor Injection
The state of DI in PHP
<?phprequire_once '/path/to/bucket.inc.php';require_once '/path/to/TalkService.php';
// create the Container$container = new bucket_Container();
// instantiate talkService from container$talkService = $container->create('TalkService');
// instantiate shared instances from container$talkService2 = $container->get('TalkService');$talkService3 = $container->get('TalkService');
![Page 37: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/37.jpg)
The state of DI in PHP
![Page 38: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/38.jpg)
Zend\Di – First steps
The state of DI in PHP
<?phpnamespace Acme;
class TalkService {public function __construct() {}
public function getTalks() {}
}
![Page 39: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/39.jpg)
Zend\Di – First steps
The state of DI in PHP
<?php
$di = new \Zend\Di\Di();
$service = $di->get('Acme\TalkService');$service->getTalks();
![Page 40: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/40.jpg)
Zend\Di – Constructor Injection
The state of DI in PHP
<?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 41: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/41.jpg)
Zend\Di – Constructor Injection
The state of DI in PHP
<?php
$di = new \Zend\Di\Di();
$service = $di->get('Acme\TalkService');$service->getTalks();
![Page 42: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/42.jpg)
Zend\Di – Setter Injection
The state of DI in PHP
<?phpnamespace Acme;
class Logger {public function doLog($logMsg) {}
}
class TalkService {public function __construct(TalkRepository $repo) {}
public function setLogger(Logger $logger) {}
public function getTalks() {}
}
![Page 43: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/43.jpg)
Zend\Di – Setter Injection
The state of DI in PHP
<?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 44: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/44.jpg)
Zend\Di – Interface Injection
The state of DI in PHP
<?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 45: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/45.jpg)
Zend\Di – Interface Injection
The state of DI in PHP
<?php
$di = new \Zend\Di\Di();
$service = $di->get('Acme\TalkService');$service->getTalks();
![Page 46: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/46.jpg)
Zend\Di – General usage
The state of DI in PHP
<?php
$di = new \Zend\Di\Di();
$service = $di->get('Acme\TalkService');var_dump($service);
$service2 = $di->get('Acme\TalkService');var_dump($service2); // same instance as $service
$service3 = $di->get('Acme\TalkService',array(
'repo' => new \phpbnl12\TalkRepository())
);var_dump($service3); // new instance
![Page 47: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/47.jpg)
Zend\Di – Builder Definition
The state of DI in PHP
<?php// describe dependency$dep = new \Zend\Di\Definition\Builder\PhpClass();$dep->setName('Acme\TalkRepository');
// describe class$class = new \Zend\Di\Definition\Builder\PhpClass();$class->setName('Acme\TalkService');
// add injection method$im = new \Zend\Di\Definition\Builder\InjectionMethod();$im->setName('__construct');$im->addParameter('repo', 'Acme\TalkRepository');$class->addInjectionMethod($im);
// configure builder$builder = new \Zend\Di\Definition\BuilderDefinition();$builder->addClass($dep);$builder->addClass($class);
![Page 48: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/48.jpg)
Zend\Di – Builder Definition
The state of DI in PHP
<?php
// add to Di$defList = new \Zend\Di\DefinitionList($builder);$di = new \Zend\Di\Di($defList);
$service = $di->get('Acme\TalkService');var_dump($service);
![Page 49: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/49.jpg)
The state of DI in PHP
![Page 50: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/50.jpg)
Symfony2
The state of DI in PHP
<?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 51: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/51.jpg)
Symfony2 – Configuration file
The state of DI in PHP
<?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>
File services.xml in src/Acme/DemoBundle/Resources/config
![Page 52: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/52.jpg)
Symfony2 – Constructor Injection
The state of DI in PHP
<?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 53: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/53.jpg)
Symfony2 – Setter Injection
The state of DI in PHP
<?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 54: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/54.jpg)
Symfony2 – Setter Injection (optional)
The state of DI in PHP
<?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 55: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/55.jpg)
Symfony2 – Property Injection
The state of DI in PHP
<?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 56: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/56.jpg)
Symfony2 – Interface Injection
The state of DI in PHP
Not supported!
![Page 57: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/57.jpg)
Symfony2 – private/public Services
The state of DI in PHP
<?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 58: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/58.jpg)
Symfony2 – Service inheritance
The state of DI in PHP
<?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 59: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/59.jpg)
Symfony2 – Service scoping
The state of DI in PHP
<?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 60: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/60.jpg)
The state of DI in PHP
![Page 61: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/61.jpg)
Flow3 – Constructor Injection
The state of DI in PHP
<?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 62: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/62.jpg)
Flow3 – Setter Injection (manually)
The state of DI in PHP
<?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 63: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/63.jpg)
Flow3 – Setter Injection (manually)
The state of DI in PHP
File Objects.yaml in Packages/Application/Acme.Demo/Configuration# @package AcmeAcme\Demo\Controller\StandardController: properties: talkService: object: Acme\Demo\Service\TalkService
![Page 64: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/64.jpg)
Flow3 – Setter Injection (Automagic)
The state of DI in PHP
<?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 65: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/65.jpg)
Flow3 – Setter Injection (Automagic)
The state of DI in PHP
<?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 66: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/66.jpg)
Flow3 – Property Injection
The state of DI in PHP
<?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 67: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/67.jpg)
Flow3 – Property Injection (with Interface)
The state of DI in PHP
<?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 68: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/68.jpg)
Flow3 – Property Injection (with Interface)
The state of DI in PHP
# @package AcmeAcme\Demo\Service\TalkServiceInterface: className: 'Acme\Demo\Service\TalkService'
File Objects.yaml in Packages/Application/Acme.Demo/Configuration
![Page 69: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/69.jpg)
Flow3 – Scoping
The state of DI in PHP
<?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 70: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/70.jpg)
Benefits
Real World Dependency Injection
![Page 71: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/71.jpg)
Benefits
Loose coupling, reuse of components!
Real World Dependency Injection
![Page 72: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/72.jpg)
Benefits
Can reduce the amount of code!
Real World Dependency Injection
![Page 73: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/73.jpg)
Benefits
Helps developers to understand the code!
Real World Dependency Injection
![Page 74: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/74.jpg)
Cons – Developers need mindshift
Configuration ↔ Runtime
Real World Dependency Injection
![Page 75: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/75.jpg)
The state of DI in PHP
Cons - PSR for DI container missing!
![Page 76: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/76.jpg)
The state of DI in PHP
Lack of IDE support
![Page 77: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/77.jpg)
Thank you!
![Page 78: The state of DI - DPC12](https://reader033.vdocument.in/reader033/viewer/2022051514/54b4fd4d4a7959b9428b4813/html5/thumbnails/78.jpg)
http://joind.in/6250