Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Dependency Injection with PHP
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Bastian Hofmann
July 18, 2012
Programming
650
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
390
Creating a fast Kubernetes Development Workflow
bastianhofmann
0
160
Highly available cross-region deployments with Kubernetes
bastianhofmann
1
190
From source to Kubernetes in 30 minutes
bastianhofmann
0
210
Introduction to Kubernetes
bastianhofmann
1
140
CI/CD with Kubernetes
bastianhofmann
0
260
Creating a fast Kubernetes Development Workflow
bastianhofmann
1
300
Deploying your first Micro-Service application to Kubernetes
bastianhofmann
2
220
Creating a fast Kubernetes Development Workflow
bastianhofmann
0
290
Other Decks in Programming
See All in Programming
TanStack Vue Query と Pinia Colada から学ぶ非同期状態の型設計
aoshi
0
640
Railsバージョンを4倍にした話
optim
0
970
AIが生成したコードは読むべきか?抽象化設計とコードの品質保証 / abstraction-design-and-quality
minodriven
16
8.9k
PyConTW 2026 - AI Coding 不只是工具升級, 區域醫院醫療資訊系統協作開發與資訊團隊轉型實踐
cclai999
0
240
Coding with AI: How to trust the storyteller (AWS Community Day Adria, Ljubljana 2026)
slobodan
0
140
Materialが消えた日 〜真っ白なFlutterキャンバスにRenderObjectで絵を描く〜
umigishiaoi
0
150
Engineering Biometric UX Across Android Versions | DroidKnights 2026
ryunen344
1
190
Optunaを作ったときにやりたかったこと、その後 (2026/10/16, Optuna Meetup #3)
iwiwi
4
1.5k
SODA - FACT BOOK(EN)
sodainc
1
220
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
410
baseline으로 마이그레이션, 리팩토링 안전하게 맡기기
fornewid
1
960
Rails初心者がパスキーを実装してみて大変だったこと
ktombow
0
160
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
143
7.9k
Mobile First: as difficult as doing things right
swwweet
225
10k
First, design no harm
axbom
PRO
2
1.4k
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
12k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.4k
Jamie Indigo - Trashchat’s Guide to Black Boxes: Technical SEO Tactics for LLMs
techseoconnect
PRO
0
740
Ecommerce SEO: The Keys for Success Now & Beyond - #SERPConf2024
aleyda
1
2.2k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
So, you think you're a good person
axbom
PRO
2
2.3k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
4
1.2k
Deep Space Network (abreviated)
tonyrice
0
410
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
60k
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