Zend_Registry - do you need getInstance ()?

Hey, I'm wondering when you access Zend_Registry in your application, if you need to enable getInstance (), and if so, why?

eg

    Zend_Registry::getInstance()->get('db');
      

against.

    Zend_Registry::get('db');
      

they both seem to work with the later, less verbose one. I am vaguely aware that Zend_Registry is a singleton, which I think means there can only be one instance of it? so why do you need getInstance ()?

+2


a source to share


4 answers


I wrote the code for Zend_Registry in 2007.

You can use static get()

and set()

, and this affects the default instance of the registry, which is stored as a static member of the class. This is for most situations.



All of the other Zend_Registry methods support pretty much non-standard situations, for example if you want to implement your own class for your registry, but still make it standalone. Or, if you want to get a real registry object, for example, so that you can serialize the entire registry object with all its contents.

+6


a source


You don't need to use getInstance()

. If you look into the class Zend_Registry

, you will see:

  public static function get($index)
    {
        $instance = self::getInstance();

        if (!$instance->offsetExists($index)) {
            require_once 'Zend/Exception.php';
            throw new Zend_Exception("No entry is registered for key '$index'");
        }

        return $instance->offsetGet($index);
    }

      



It gets an instance for you in static methods.

+2


a source


No, you don't need to use getInstance()

. It's there, I believe, for other areas of the API. It is also a general outline when creating singlons to expose a method getInstance()

to use to get an instance of that singleton. So he decided to stick with this pattern.

0


a source


You might want to use Zend_Registry :: getInstance () if you want to access the number of calls to the registry; instead of going through the offsetExists () method and subsequent calls to array_key_exists () for each property, you get an instance and then step through the ArrayObject methods to get the saved entries.

Another reason why you might want to use Zend_Registry :: getInstance () is if you keep the registry in a cache.

Other than that, you can find Zend_Registry :: get () for whatever you need.

0


a source







All Articles