Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Speaker Deck
PRO
Sign in
Sign up for free
Dependency Injection with PHP
Bastian Hofmann
October 16, 2012
Programming
9
470
Dependency Injection with PHP
Bastian Hofmann
October 16, 2012
Tweet
Share
More Decks by Bastian Hofmann
See All by Bastian Hofmann
Monitoring in Kubernetes with Prometheus and Grafana
bastianhofmann
0
170
Creating a fast Kubernetes Development Workflow
bastianhofmann
0
70
Highly available cross-region deployments with Kubernetes
bastianhofmann
1
96
From source to Kubernetes in 30 minutes
bastianhofmann
0
97
Introduction to Kubernetes
bastianhofmann
1
140
CI/CD with Kubernetes
bastianhofmann
0
120
Creating a fast Kubernetes Development Workflow
bastianhofmann
1
220
Deploying your first Micro-Service application to Kubernetes
bastianhofmann
2
120
Creating a fast Kubernetes Development Workflow
bastianhofmann
0
120
Other Decks in Programming
See All in Programming
Scrum Fest Osaka 2022/5年で200人になったスタートアップの アジャイル開発の歴史とリアル
atamaplus
1
800
Vite でお手軽 Vue.js の環境構築
azuki
2
170
シェーダー氷山発掘記
logilabo
0
140
【Scrum Fest Osaka 2022】スクラムチームに放り込まれた若手エンジニアの皆さん、どのように技術のキャッチアップをしていくかイメージはついていますか?
miiiki
0
110
Java アプリとAWS の良い関係 - AWS でJava アプリを実行する一番簡単な方法教えます / AWS for Javarista
kanamasa
2
1.2k
Git・Git-Flowについて
nerusan_main
0
410
Mobile Product Engineering
championswimmer
0
290
What's new in Android development tools まとめ
mkeeda
0
270
エンジニアによる事業指標計測のススメ
doyaaaaaken
1
180
Node.jsデザインパターンを読んで
mmmommm
0
1.7k
Airflowはすごいぞ!
hankehly
0
370
Independently together: better developer experience & App performance
bcinarli
0
160
Featured
See All Featured
Fontdeck: Realign not Redesign
paulrobertlloyd
73
4.1k
Fireside Chat
paigeccino
11
1.3k
Pencils Down: Stop Designing & Start Developing
hursman
112
9.8k
The Language of Interfaces
destraynor
148
20k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
226
15k
Design by the Numbers
sachag
271
17k
For a Future-Friendly Web
brad_frost
166
7.4k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
39
13k
Navigating Team Friction
lara
175
11k
How STYLIGHT went responsive
nonsquared
85
3.9k
Three Pipe Problems
jasonvnalue
89
8.7k
Web Components: a chance to create the future
zenorocha
303
40k
Transcript
Dependency Injection with PHP @BastianHofmann
None
None
None
?
Dependency Injection Dependency Injection Containers Dependency Inversion Dependencies
None
None
ResearchGate gives science back to the people who make it
happen. We help researchers build reputation and accelerate scientific progress. On their terms. ‟
None
None
None
None
None
None
None
Questions? Ask!
http://speakerdeck.com/u/bastianhofmann
Dependency Inversion
http://www.doolwind.com/blog/solid-principles-for-game-developers/
http://qafoo.com/presentations.html
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
None
None
Dependency Injection
Constructor
None
None
Setter
None
Method argument
None
Constructing classes
None
Benefits
None
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
None
Creating an instance of a class
None
None
None
PHP 5.5
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
Providers
@providedBy
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
Great! So, how does it work?
Development: Reflection
Production: Generated Factory Classes
None
None
https://github.com/bashofmann/dic_demo/
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
https://github.com/bashofmann/dic_demo/
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'); } }
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 mail@bas2anhofmann.de Did you like this
talk? https://joind.in/7346