There are many global variables in the project process, which need to be stored globally. Are global variables used for storage? That would be too weak. Zend uses the Registry mechanism (registry) to store objects and values, and is a container for storing objects and values.
The Zend_Registry class is used for this purpose
Code Example
Zend_Registry::set('config', $config);
Zend_Registry::get('config');
Code Analysis
These two functions are the two most commonly used functions. Let’s take a look at this class
class Zend_Registry extends ArrayObject
This class inherits from ArrayObject
ArrayObject implements IteratorAggregate , Traversable , ArrayAccess , Serializable , Countable
ArrayObject is a collection of objects, equivalent to the concept of generic collections in other languages.
Focus on the void ArrayObject::offsetSet (mixed $index, mixed $newval). This function is to set the key and value in the hashtable. It is just the key and the value can be of any type.
Okay, go back to Zend_Registry and see what set has done
set function
The offset method is easy to understand, but why use the getInstance method?
I suggest you take a closer look. This is a singleton pattern combined with class static methods.
Our general singleton pattern is written as:
The register here can be called directly using static methods
A::setVal();
I wrote a demo for the general code idea