Superglobals are bad for you. Why would you ever want the service manager
as superglobal?

Keep things in your scope, inject wherever possible and avoid leaking out
of your class' responsibilities.
Things get ugly really fast with static variables and global variables, so
simply don't do it!

So yes, the solution is injecting the config (mustn't be the global one,
you can inject $config['your_stuff']) and generally what you need in your
models/services. That's IOC basics and there's nothing bad about it :)

I personally don't like `ServiceLocatorAware` (and other `Aware`
interfaces) either, so I personally just inject everything I need at
`__construct`.

Marco Pivetta

http://twitter.com/Ocramius

http://ocramius.github.com/


On 19 February 2013 10:37, roberto blanko <robertobla...@gmail.com> wrote:

> Hey devs,
>
> one problem drives me nuts, since I started working with ZF2 (never worked
> with Version 1):
>
> I'm in need of the service manager all the time. E.g. to access my config
> in config/autoload/local.php via $sm->get('Config'). I need the service
> manager everywhere. Controllers, models, you name it. And I don’t want to
> pass it around by hand, which would make everything ugly.
>
> Now I’ve started to implement ServiceLocatorAwareInterface in most classes.
> But this has the two downsides for me:
>
> 1. The classes which make use of my ServiceLocatorAware classes need to
> have acces to the service locater, as well, in order to instantiate the
> objects. So even more ServiceLocatorAware classes and even more invokables
> to be added to module.config.php.
>
> 2. The service manager is a property of all ServiceLocatorAware classes,
> which can be difficult if you want to persist a ServiceLocatorAware model
> e.g. to the session.
>
> Now I'm starting to get the feeling the I haven’t really understood ZF2 and
> the service manager. Injecting everything cannot be the solution, can it?
> Should I really inject e.g. my global config into all classes? And if you
> don’t want to have plain entity models but business models with some
> process logic in them, you cannot abstain from the service manager either.
>
> I would really appreciate any clarification on this topic. And I get the
> impression a lot of others do, too.
>
> Regards
> Rob
>

Reply via email to