Skip to main content

Posts

Showing posts from September, 2012

PHP Registry design pattern

Registry Pattern What if we could treat ordinary classes as limited-instance classes, without having to make them Singletons? The Registry is a Singleton, used to store instance of other “normal” classes. Another way to think of it is as a global key/instance storeroom. Instances of non-Singleton classes are given a key (identifier) and are “kept” inside the Registry’s private storage. When requests are made for specific keys, the Registry checks whether there are any instances for those keys, and returns them. If no instance is found at a key, the Registry’s methods will return a default value. You might be wondering what practical use this is to our framework. There are many times when we will need to limit the amount of class instances, as they relate to shared connections to third-party services or shared resources. Consider our cache system. For most applications, one cache provider will be sufficient. You will, for instance, connect to one Memcached server. We certainly d