Do you find Zend_Registry useful?
For which tasks it should be used? For which not?
Global state for variables is not a good practice.
Main objects may have global state injected via $front->setParam('paramName', $object)
,
so what's the purpose of Zend_Registry?.
Quoting PoEAA on Registry Pattern:
The main reason why I use the registry (when I use it) is because it creates an easily accessible application scope. With the registry, I don't have to litter objects all over the global scope; only the Registry itself is global. It's convenient to lookup whatever I've thrown into the Registry from everywhere, including the model:
However, just like with Singletons, Registry is often frowned upon. Here is an article by Brandon Savage with some thought about why not to use the Registry. The main arguments against the Registry are
Those who vote against the Registry usually advocate the usage of Dependency Injection, although it should be noted that you can inject the Registry as well once you got an instance of it. You do not have Inversion of Control then though, because the using object will pull what it needs from the Registry. Using the Registry as a Service Locator is a valid approach though.
See this article by Martin Fowler about ServiceLocator vs. Dependency Injection.
Like pointed out in the comments to your question,
Zend_Registry
is not a strict Singleton. You can instantiate multiple instances where needed in addition to using the global instance you get withZend_Registry::getInstance()
. So objects can have their own registry. But when using Registry this way, it's basically just a glorified ArrayObject.Final note: just like with all Design Patterns, use it if applicable to your problem.
When you are using
$front->setParam
, you are defining a parameter in the Front Controller.But that parameter is not available in (or should not be used from) other layers of the application, such as the Model.
Zend_Registry
, like any other global variable, is available from anywhere in your application -- including inside the Model.But using a Registry instead of a bunch of global variables ensures you won't have many global variables everywhere : even if using a Registry implies some global state (which is not that good, one should say), it's better to have that all in one place.
A couple of real life examples could be :
In the end, do I find
Zend_Registry
useful ?Well, when it comes to having some global state, yes, it's useful.
But if it's usage can be avoided, it might be better : conceptually speaking, not having your classes depend on any global state is better : easier to re-use, easier to test, ...
About that, you might want to take a look at what Dependency Injection is.
I agree with Pascal MARTIN. I just started accustoming myself to Dependency Injection. But I haven't found a way to inject objects in controllers, so what I recently did was use de registry to access a service in the controller. In a current project I'm doing the following:
bootstrap:
...then in a controller:
Hopefully this is a useful example for you.