Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Dependency Injection

Dependency Injection

Quick Talk given at Atlanta PHP on what Dependency Injection is.

Shawn Stratton

May 23, 2012
Tweet

More Decks by Shawn Stratton

Other Decks in Programming

Transcript

  1. What I’ll Cover 2 I will cover: • What DI

    isn’t • What DI is • Why you should use DI
  2. What I’ll Cover 2 I will cover: • What DI

    isn’t • What DI is • Why you should use DI I won’t cover:
  3. What I’ll Cover 2 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
  4. What I’ll Cover 2 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
  5. What I’ll Cover 2 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)
  6. A Design Style 8 <?php / / Just Some Class

    class Dependant { protected $db ; protected $dependency ; public function __construct(PDO $db , Depdendency $dependency) { $this−>db = $db ; $this−>dependency = $dependency ; } public function somefunc ( ) { / / Use Dependencies } }
  7. Easy 9 <?php / / This i s Meta Code

    Only ( not a concrete Implementation ) / / Create Locator , pass a mappings f i l e or var / / Note Dependency would be defined here as would / / Dependant $sl = new Container( ’ / path / to / mappings / f i l e ’ ) ; / / Create PDO (we don ’ t want to map i t ) $pdo = new PDO( ’ dsn ’ ) ; $sl−>defineSingleton( ’PDO ’ , $pdo) ; $dependant = $sl−>get( ’ Dependant ’ ) ; / / Dependant i s type of Dependant
  8. Service Locator 12 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
  9. Service Locators Detail 13 • 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
  10. Another Analogy 15 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
  11. DI Container Detail 16 • 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)
  12. Makes Testing Easy 18 <?php class DependantTest extends PHPUnit_Framework_TestCase {

    protected $dependant ; protected function setUp ( ) { $pdo = new PDO( ’ s q l i t e dsn ’ ) ; $dependency = $this−>getMock( ’ Dependency ’ , array ( ’ someFunction ’ ) ) ; $dependency−>expects( $this−>once ( ) )−>method( ’ someFunction ’ ) ; $this−>dependant = new Dependant($pdo , $dependency) ; } }
  13. Easy Extension 19 Steps to extend and use a class:

    1. Create class b and have it extend class a
  14. Easy Extension 19 Steps to extend and use a class:

    1. Create class b and have it extend class a 2. Change Mapping
  15. Easy Extension 19 Steps to extend and use a class:

    1. Create class b and have it extend class a 2. Change Mapping 3. Profit!
  16. Enforces Interfaces 21 <?php / / Inteface Defining the ”

    Math ” Api i n t e r f a c e Math { public function add($a , $b) ; public function sub($a , $b) ; public function multiply($a , $b) ; public function divide($a , $b) ; } / / 2+2 = 5 f o r large values of 2 / / ( see Thinkgeek s h i r t s ) class HeavyMath implements Math { public function add($a , $b) { return ($a == 2 && $b == 2) ? 5 : $a+$b ; } }
  17. Mapping Files 22 <?php return array ( ’Foo ’ =>

    array ( ’ class ’ => ’ Zend Foo ’ , ’ arguments ’ => array ( ’ c o n s t r u c t ’ => ’ComponentA ’ ) , ) , ’ComponentA ’ => array ( ’ class ’ => ’ Zend Foo Component A ’ , ’ instanceof ’ => ’ Zend Foo Component Interface ’ , ) ) ; Figure 6: Zend DI Proposal by Frederic Cargnelutti (mod- ified)
  18. Thank You 23 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!