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)
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
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
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
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)
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 ; } }
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!