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
October 16, 2012
Programming
600
9
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Dependency Injection with PHP
Bastian Hofmann
October 16, 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
200
Introduction to Kubernetes
bastianhofmann
1
140
CI/CD with Kubernetes
bastianhofmann
0
250
Creating a fast Kubernetes Development Workflow
bastianhofmann
1
290
Deploying your first Micro-Service application to Kubernetes
bastianhofmann
2
210
Creating a fast Kubernetes Development Workflow
bastianhofmann
0
270
Other Decks in Programming
See All in Programming
鹿野さんに聞く!『TypeScriptコードレシピ集』で磨く実践力
tonkotsuboy_com
4
1k
1B+ /day規模のログを管理する技術
broadleaf
0
130
AIを活用したE2Eテスト実装効率化のあゆみ / ebisu-mobile-14-kotetu
kotetuco
0
170
エンジニアにデザインハーネスを 〜デザインプロセスを規定するためのハーネス〜 / Design harness from an engineer's perspective
rkaga
2
1.3k
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
560
ローカルLLMでどこまでコードが書けるか -縮小版 / How much code can be written on a local LLM Shortened
kishida
2
180
地域 SRE コミュニティ最前線 - ホンマでっかSRE勉強会
tk3fftk
0
210
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
250
「正の参照」と 「負の導出」で組む ハーネスエンジニアリング
cottpan
1
130
AI 輔助遺留系統現代化的經驗分享
jame2408
1
1.2k
例外の正しい扱い方 そのエラー try-catchして大丈夫?
jinwatanabe
0
360
Signal Forms: Details & Live Coding @enterJS 2026 in Mannheim
manfredsteyer
PRO
0
220
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
260
Large-scale JavaScript Application Architecture
addyosmani
515
110k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
1
1.5k
Git: the NoSQL Database
bkeepers
PRO
432
67k
Unsuck your backbone
ammeep
672
58k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.8k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
360
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
1
760
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
210
Producing Creativity
orderedlist
PRO
348
40k
Designing Powerful Visuals for Engaging Learning
tmiket
1
440
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6k
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
[email protected]
Did you like this
talk? https://joind.in/7346