david91 Posted December 23, 2012 Share Posted December 23, 2012 In the application I am coding myself (MVC) I am using dependency injection to pass an object its dependencies. I don't want to use the Singleton pattern or a dependency injection container. When the application starts up it has some procedural code instantiating important objects like this: (It loads up more objects than below but they should be enough to explain what I am on about) /* * ------------------------------------------------------ * INSTANTIATE THE APPLICATION CLASS * ------------------------------------------------------ */ $app = new App(); $app->environment('development'); /* * ------------------------------------------------------ * INSTANTIATE THE CONFIGURATION CLASS * ------------------------------------------------------ */ $config = new Config(); /* * ------------------------------------------------------ * INSTANTIATE THE LOGGER CLASS * ------------------------------------------------------ */ $logger = new Logger($config); /* * ------------------------------------------------------ * INSTANTIATE THE LOCALE CLASS * ------------------------------------------------------ */ $locale = new Locale(); /* * ------------------------------------------------------ * INSTANTIATE THE HTTP RESPONSE CLASS * ------------------------------------------------------ */ $httpResponse = new HttpResponse(); /* * ------------------------------------------------------ * INSTANTIATE THE ROUTER CLASS * ------------------------------------------------------ */ $router = new Router($config, $logger, $httpResponse); $router->route(); As you can see the $logger object depends on the $config object and I inject the $config object into the $logger objects constructor. Now, as you can see when I am instantiating the $router object it has 3 dependencies ($config, $logger and $httpResponse). I just feel it's weird how the $logger object holds the $config object within it so anywhere I inject the $config object and the $logger object into an object like in the $router object I feel like I'm injecting the $config object twice and that it's wrong. Obiously when I have more and more objects and dependencies it will seem like I am injecting the same object more than two or three times and that will really make me wonder if I'm doing it wrong. Is what I'm doing ok? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/272310-dependency-injection-it-works-this-way-but-something-tells-me-im-doing-it-wrong/ Share on other sites More sharing options...
ignace Posted December 23, 2012 Share Posted December 23, 2012 Quote Is what I'm doing ok? Sure, you are only passing references any way not entire objects. Quote Link to comment https://forums.phpfreaks.com/topic/272310-dependency-injection-it-works-this-way-but-something-tells-me-im-doing-it-wrong/#findComment-1401028 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.