Zend_Registry: real life examples

Do you find Zend_Registry useful?

What tasks should it be used for? Why not?

The global state of variables is not good practice. Basic objects can have a global state, $front->setParam('paramName', $object) through $front->setParam('paramName', $object) , so what is the purpose of Zend_Registry ?.

+6
php global-variables zend-framework
source share
3 answers

Quoting PoEAA in a registry template :

When you want to find an object, you usually start with another object that has a connection with it, and use the link to go to it. Thus, if you want to find all orders for a customer, you start with the customer’s object and use the method to get orders. However, in some cases, you will not have a suitable object to start with. You can find out the customer number, but do not have a link. In this case, you need some kind of search method - the finder, but the question remains: how do you get to the finder?

The main reason I use the registry (when I use it) is because it creates an easily accessible area of ​​the application. With the registry, I do not need to put objects in the entire global space; only the registry itself is global. It is convenient to search for everything that I threw into the registry, everywhere, including the model:

  • Zend_Cache, Zend_Translate, important application paths, etc.

However, as with Singletons, the Registry is often frowned upon. Here is an article by Brandon Savage with some thoughts on why not using the registry . The main arguments against the registry:

  • it simplifies device testing
  • inexperienced coders can get into it too much and do not care about the right design

Those who vote against the Registry usually favor using the Injection of Dependency , although it should be noted that you can also enter the registry at the same time you have an example of this. However, you do not have Inversion of Control , because the used object will pull out what it needs from the registry. However, using the registry as a service locator is acceptable.

See the article by Martin Fowler on implementing ServiceLocator against injection .

As pointed out in the comments on your question, Zend_Registry not a strict Singleton. You can create multiple instances where necessary, in addition to using the global instance that you get with Zend_Registry::getInstance() . Thus, objects can have their own registry. But when using Registry in this way, it is basically just an illustrious ArrayObject.

Final note: as with all design patterns, use it if applicable to your problem.

+9
source share

When you use $front->setParam , you define the parameter in Front Controller.

But this option is not available (or should not be used) by other layers of the application, for example Model.

Zend_Registry , like any other global variable, is accessible from anywhere in your application, including inside the model.

But using the registry instead of a combination of global variables ensures that you will not have many global variables everywhere: even if using the registry implies some kind of global state (which is not so good, I must say), it is better to have everything in one place.


A few examples of real life might be:

  • Save the database connection that is installed in bootsreap but is used in models
  • Store globally the adapter (or any mechanism) that will be used for translation / localization in all layers of your application. Zend Framework itself does this.


In the end, can I find Zend_Registry useful?

Well, when it comes to having some global state, yes, it is helpful.

But if this can be avoided, it could be better: conceptually speaking, no better than your classes depending on any global state: easier to reuse, easier to test ...
You may want to take a look at what Injection of Dependency is about this .

+10
source share

I agree with Pascal MARTIN . I just started to accustom myself to Injection Dependency. But I did not find a way to inject objects into the controllers, so I recently used the registry to access the service in the controller. In the current project, I am doing the following:

self-loading:

 // simple DI without an IoC container, and what have you $dbAdapter = Zend_Db::factory( /* bla bla */ ); $mediaService = new Service_Media( new Repository_Media_Db( $dbAdapter ) ); $registry = Zend_Registry::getInstance(); $registry->mediaService = $mediaService; 

... then in the controller:

 public function init() { $this->_mediaService = Zend_Registry::get( 'mediaService' ); } public function listAction() { // simplified $this->view->media = $this->_mediaService->listAllUploadedVideos(); } 

Hope this is a useful example for you.

+4
source share

All Articles