Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Dependency Injection with PHP
Search
Bastian Hofmann
July 18, 2012
Programming
630
5
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Dependency Injection with PHP
Bastian Hofmann
July 18, 2012
More Decks by Bastian Hofmann
See All by Bastian Hofmann
Monitoring in Kubernetes with Prometheus and Grafana
bastianhofmann
0
360
Creating a fast Kubernetes Development Workflow
bastianhofmann
0
150
Highly available cross-region deployments with Kubernetes
bastianhofmann
1
170
From source to Kubernetes in 30 minutes
bastianhofmann
0
190
Introduction to Kubernetes
bastianhofmann
1
140
CI/CD with Kubernetes
bastianhofmann
0
240
Creating a fast Kubernetes Development Workflow
bastianhofmann
1
280
Deploying your first Micro-Service application to Kubernetes
bastianhofmann
2
200
Creating a fast Kubernetes Development Workflow
bastianhofmann
0
270
Other Decks in Programming
See All in Programming
今さら聞けないCancellationToken
htkym
0
220
TAKTでAI駆動開発の品質を設計する
j5ik2o
6
930
These Five Tricks Can Make Your Apps Greener, Cheaper, & Nicer
hollycummins
0
270
プラグインで拡張される Context をtype-safe にする難しさと設計判断
kazupon
2
590
不変条件と整合性境界—ビジネスが決める設計判断と実現パターン / Invariants and Consistency Boundaries
nrslib
13
3.4k
脅威をエンジニアリングの糧にして――現場編 / Turning Threats into Engineering Fuel — Field Edition
nrslib
0
250
tsserverとは何だったのか、これからどうなるのか
nowaki28
1
450
[2026年度第1回ORセミナー] 計画最適化ベンチャーと競技プログラミング人材
terryu16
0
250
New "Type" system on PicoRuby
pocke
1
470
AIエージェントの隔離技術の徹底比較
kawayu
0
460
権限チェックの一貫性を型で守る TypeScript による多層防御
mnch
4
1.1k
JavaDoc 再入門
nagise
0
280
Featured
See All Featured
Why Our Code Smells
bkeepers
PRO
340
58k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
580
SEO for Brand Visibility & Recognition
aleyda
0
4.6k
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
1
770
The Director’s Chair: Orchestrating AI for Truly Effective Learning
tmiket
1
190
Building the Perfect Custom Keyboard
takai
2
780
How To Speak Unicorn (iThemes Webinar)
marktimemedia
1
480
The Hidden Cost of Media on the Web [PixelPalooza 2025]
tammyeverts
2
320
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
370
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
47
8.2k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
400
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Transcript
Dependency Injection with PHP @BastianHofmann
None
None
?
None
None
None
None
None
None
None
Dependency Inversion
http://www.doolwind.com/blog/solid-principles-for-game-developers/
http://www.doolwind.com/blog/solid-principles-for-game-developers/
High-level modules should not depend on low level modules but
on abstractions. ”
A B C
A B C <<interface>> <<interface>>
A B C <<interface>>
A B Adapter <<interface>> C
Dependencies
None
Dependency Injection
Constructor
None
None
Setter
None
Method argument
None
Constructing classes
None
Benefits
Easier to change
Easier testing
Only test the class, not the dependencies
None
Mocking
None
None
http://www.doolwind.com/blog/solid-principles-for-game-developers/
But...
None
Make it easier with factories
None
Dependency Injection Container
None
rg\injektor inspired by google-guice
https://github.com/researchgate/injektor
http://getcomposer.org/ http://packagist.org/
$ cat composer.json
$ wget http://getcomposer.org/composer.phar $ php composer.phar install Installing dependencies -
Installing monolog/monolog (1.1.0) Downloading: 100% ... monolog/monolog suggests installing mlehner/ gelf-php (Allow sending log messages to a GrayLog2 server) Writing lock file Generating autoload files
Creating an instance of a class
None
None
None
Automatic creation of dependencies through Typehints
None
@inject
None
What classes can be injected?
No constructor
None
Constructor without arguments
None
Constructor with arguments that have default values
None
Injectable constructor
None
None
By the way: Property Injection
None
Singletons with public static getInstance method following the same rules
None
STOP: Don‘t use Singeltons
Singletons are evil!
Let the DIC take care of instances
@singleton
None
Passing additional, fixed values to instance
None
Working with Interfaces
None
But we can not create an instance of an Interface!
@implementedBy
None
Named @implementedBy
None
None
Method invocation at DIC
None
Same rules
None
None
And if you don‘t like annotations... ... or want to
integrate 3rd party code
None
None
None
None
DEMO
None
https://github.com/symfony/DependencyInjection
None
None
None
https://github.com/bashofmann/dic_demo/
DEMO
None
https://github.com/zendframework/zf2
None
None
Automatic creation of dependencies through Typehints
DEMO
AOP
http://flow3.typo3.org/documentation/guide/partiii/ aspectorientedprogramming.html
class Forum { /** * @FLOW3\Inject
* @var ApplicationLoggerInterface */ protected $applicationLogger; /** * @param Post $post * @return void */ public function deletePost(Post $post) { $this-‐>applicationLogger-‐>log('Removing post'); $this-‐>posts-‐>remove($post); } }
class Forum { /** * Delete a
forum post * * @param Post $post * @return void */ public function deletePost(Post $post) { $this-‐>posts-‐>remove($post); } }
/** * @FLOW3\Aspect */ class LoggingAspect { /**
* @FLOW3\Inject * @var ApplicationLoggerInterface */ protected $applicationLogger; /** * @FLOW3\Before("method(Forum-‐>deletePost())") */ public function logDeletePost(\TYPO3\FLOW3\AOP \JoinPointInterface $joinPoint) { $post = $joinPoint-‐>getMethodArgument('post'); $this-‐>applicationLogger-‐>log('Removing post); } }
rg\injektor inspired by google-guice
None
h"p://twi"er.com/Bas2anHofmann h"p://profiles.google.com/bashofmann h"p://lanyrd.com/people/Bas2anHofmann h"p://speakerdeck.com/u/bas2anhofmann h"ps://github.com/bashofmann
[email protected]
Did you like this
talk? http://bit.ly/oscon2012_di_php