On Fri, Aug 23, 2013 at 5:21 PM, Philip G <[email protected]> wrote: > On Fri, Aug 23, 2013 at 3:05 PM, Matthew Weier O'Phinney > <[email protected]>wrote: > >> Just for argument's sake, why are you using Zend\Di instead of >> Zend\ServiceManager? Is there a compelling reason? >> >> > Purely for the sake of a co-worker's request and desire. He didn't like > hard coding of a ServiceManager alias name, even if it was an alias. He > comes from a Java background and rather see DI than SM aliases. > > However, if there is a compelling reason to use SM over DI with ZFW, I > could easily convince him otherwise. > > In my example, I originally use $sm->get('Firmware\PlatformObject'); and it > does work perfectly. And he just rather see: $this->getPlatformObject() > where platformObject is injected into our controller by the framework. I > wanted to compare the two different approaches, and select the best option.
So, several things are getting conflated here. First, the way we recommend using the ServiceManager is as an Inversion of Control (IoC) container -- which, if done correctly, ends up looking exactly like your co-worker wants. You create service definitions for each object, and any object that needs dependencies either has a factory, or makes use of initializers, in order to inject them. That addresses the `$this->getPlatformObject()` use case -- as your factory and/or initializers are injecting it for you. Second, regarding aliases: we typically recommend using fully qualified class names in the service manager as well, with the one exception being several of the well-known MVC services (Application, ControllerManager, etc.). We recommend using interface names for aliases, which then allows you to switch implementations without needing to replace existing factories. Now, for the compelling reasons to use SM over DI in ZF2: - it's more performant - configuration is more straight-forward (you're typically writing factories in PHP, rather than creating metadata trees) - completely predictable (DI, due to the nature of the lookups, often is not) - easy to debug (put breakpoints in your factories, vs debugging framework code) - used everywhere (all plugin manager implementations are specific SM instances) -- Matthew Weier O'Phinney Project Lead | [email protected] Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc -- List: [email protected] Info: http://framework.zend.com/archives Unsubscribe: [email protected]
