5. What I’ll Cover
I will cover:
• What DI isn’t
• What DI is
• Why you should use DI
2
6. What I’ll Cover
I will cover:
• What DI isn’t
• What DI is
• Why you should use DI
I won’t cover:
2
7. What I’ll Cover
I will cover:
• What DI isn’t
• What DI is
• Why you should use DI
I won’t cover:
• How to implement DI in your project
2
8. What I’ll Cover
I will cover:
• What DI isn’t
• What DI is
• Why you should use DI
I won’t cover:
• How to implement DI in your project
• Differences in available containers
2
9. What I’ll Cover
I will cover:
• What DI isn’t
• What DI is
• Why you should use DI
I won’t cover:
• How to implement DI in your project
• Differences in available containers
• How to cook awesome bacon (ask Jeff)
2
15. A Design Style
<?php
/ / J u s t Some Class
c l a s s Dependant {
p r o t e c t e d $db ;
p r o t e c t e d $dependency ;
p u b l i c f u n c t i o n __construct ( PDO $db , Depdendency
$dependency ) {
$this−>db = $db ;
$this−>dependency = $dependency ;
}
p u b l i c f u n c t i o n somefunc ( ) {
/ / Use Dependencies
}
}
8
16. Easy
<?php
/ / T h i s i s Meta Code Only ( n o t a c o n c r e t e I m p l e m e n t a t i o n )
/ / Create Locator , pass a mappings f i l e o r v a r
/ / Note Dependency would be d e f i n e d here as would
/ / Dependant
$sl = new Container ( ’ / path / t o / mappings / f i l e ’ ) ;
/ / Create PDO ( we don ’ t want t o map i t )
$pdo = new PDO ( ’ dsn ’ ) ;
$sl−>defineSingleton ( ’PDO ’ , $pdo ) ;
$dependant = $sl−>get ( ’ Dependant ’ ) ;
/ / Dependant i s t y p e o f Dependant
9
19. Service Locator
Service Location is like ordering with
substitutions, and having the waiter completely
ignore the substitutions; you get what’s on the
menu, nothing more, nothing less.
Figure 4: Matthew Weier O‘Phinney on Service Locators
12
20. Service Locators Detail
• It’s a fancy registry.
• Inject the locator into the class via contstructor, call
the the locator to find your services.
• Works, but it’s not foolproof
13
22. Another Analogy
Dependency Injection is like ordering off the
menu – but specifying things like, ”I’d like to
substitute portabella mushrooms for the patties,
please.” The waiter then goes and brings your
dish, which has portabella mushrooms instead
of the hamburger patties listed on the menu.
Figure 5: Matthew Weier O‘Phinney on DI Containers
15
23. DI Container Detail
• Still a fancy registry, basically just a Service Locator.
• Instantiates new classes by resolving and injecting
their dependencies.
• Very Clean in regards to separation of concerns.
• Not required to run the system (you can do this
manually, trust me)
16
25. Makes Testing Easy
<?php
c l a s s DependantTest extends PHPUnit_Framework_TestCase
p r o t e c t e d $dependant ;
p r o t e c t e d f u n c t i o n setUp ( ) {
$pdo = new PDO ( ’ s q l i t e dsn ’ ) ;
$dependency = $this−>getMock ( ’ Dependency ’ ,
a r r a y ( ’ someFunction ’ ) ) ;
$dependency−>expects ( $this−>once ( ) )−>method (
’ someFunction ’ ) ;
$this−>dependant = new Dependant ( $pdo , $dependency ) ;
}
}
18
31. Enforces Interfaces
<?php
/ / I n t e f a c e D e f i n i n g t h e ” Math ” Api
i n t e r f a c e Math {
p u b l i c f u n c t i o n add ( $a , $b ) ;
p u b l i c f u n c t i o n sub ( $a , $b ) ;
p u b l i c f u n c t i o n multiply ( $a , $b ) ;
p u b l i c f u n c t i o n divide ( $a , $b ) ;
}
/ / 2+2 = 5 f o r l a r g e v a l u e s o f 2
/ / ( see Thinkgeek s h i r t s )
c l a s s HeavyMath implements Math {
p u b l i c f u n c t i o n add ( $a , $b )
{
r e t u r n ( $a == 2 && $b == 2 ) ? 5 : $a+$b ;
}
21
32. Mapping Files
<?php
return array (
’ Foo ’ => a r r a y (
’ class ’ => ’ Zend Foo ’ ,
’ arguments ’ => a r r a y ( ’ c o n s t r u c t ’ => ’ ComponentA ’ ) ,
),
’ ComponentA ’ => a r r a y (
’ class ’ => ’ Zend Foo Component A ’ ,
’ instanceof ’ => ’ Zend Foo Component Interface ’ ,
);
Figure 6: Zend DI Proposal by Frederic Cargnelutti (mod-
ified)
22
33. Thank You
More Resources:
• Martin Fowler on Inversion of Control -
http://martinfowler.com/articles/injectio
• Ralph Schindler on Learning Dependency Injection -
http://bit.ly/php-di
• Sebastian Bergmann has an awesome book called
Real-World Solutions for Developing High-Quality
PHP Frameworks and Applications
Ask Luke Allison about his Amazing Horse!
23