- 06/30/09 17:15:05 (4 years ago)
r19700 r19710 13 13 >skip this chapter and start reading the next one. 14 14 15 Dependency Injection is probably one of the most dead simple design pattern . 16 And odds are you have probably already used Dependency Injection. Butit is 17 also one of the most difficult oneto explain well. This is probably partly 15 Dependency Injection is probably one of the most dead simple design pattern 16 it is 17 also one of the most difficult to explain well. This is probably partly 18 18 due to the nonsense examples used in most introductions to Dependency 19 Injection. In this chapter, we have tried to come up with examples that fit s 19 Injection. In this chapter, we have tried to come up with examples that fit 20 20 the PHP world better. As PHP is a language mainly used for web development, 21 21 we are going to use simple Web examples. … … 210 210 211 211 >**TIP** 212 >As any other design pattern, Dependency Injection 212 >As any other design pattern, Dependency Injection 213 213 >also has some anti-patterns. The 214 214 >[Pico Container website](http://www.picocontainer.org/) r19708 r19710 12 12 a framework for instance). 13 13 14 If you remember the example ofthe first chapter, creating a `User` object 15 required to first create a `SessionStorage` object. Not a big deal, but still, 14 If you remember the example the first chapter, creating a `User` object 15 required to first create a `SessionStorage` object. Not a big deal, but still, 16 16 you have to know about all the dependencies you need before creating the 17 17 object you need: … … 26 26 Framework examples. 27 27 28 The Zend Framework `Mail` library, which ease emailsmanagement, uses the PHP 28 The Zend Framework `Mail` library, which ease management, uses the PHP 29 29 `mail()` function by default to send emails, which is not really flexible. 30 30 Thankfully, it is quite easy to change this behavior by providing a transport … … 162 162 $mailer = $container->getMailer(); 163 163 164 Last, but not theleast, each time you want to get a mailer, you don't need a 164 Last, but not least, each time you want to get a mailer, you don't need a 165 165 new instance of it. So, the container can be changed to always return the same 166 166 object: r19708 r19710 90 90 91 91 That's not much, but it will give us a more powerful and clean interface. Here 92 isthe main changes we made: 92 the main changes we made: 93 93 94 94 * The method names have been suffixed with `Service`. By convention, a … … 192 192 That's why, most of the time, you don't use the `sfServiceContainer` class 193 193 directly. It was nonetheless important to take some time to describe it as it 194 is the corner 194 is the cornerstone of the Symfony dependency injection container 195 195 implementation.