
DI and Service Locator
This article was originally published on bmf-tech.com . Overview Summarizing the differences between DI and Service Locator What is DI A type of design pattern Dependency Injection Separates dependencies between objects Ensures necessary objects are injected at runtime Makes testing easier Implementing the DI Pattern Let's implement the DI pattern (Constructor Injection). Note that the DI pattern includes methods other than constructor injection, such as setter injection and method injection. For comparison, both non-DI and DI patterns will be implemented. Non-DI Pattern <?php class SlackNotification { public function notify ( string $message ) { echo $message ; return $this ; } } class Application { protected $message ; public function __construct () { $this -> notification = new SlackNotification (); } public function alert ( string $message ) { $this -> notification -> notify ( $message ); } } // client $application = new Application (); $application -> alert ( 'slack alert' ); DI P
Continue reading on Dev.to
Opens in a new tab


