To be frank, building a Singleton array-based registry is indeed a straightforward process that can be mastered with minor hassles. Still not convinced of this? Well, in that case take a look at the definition of the following class, which hopefully will make your doubts vanish. Here it is: (ArrayRegistry.php)
<?php
class ArrayRegistry extends AbstractRegistry { private $_data = array();
// save new data to the array registry public function set($key, $value) { $this->_data[$key] = $value; }
// get data from the array registry public function get($key) { return isset($this->_data[$key]) ? $this->_data[$key] : NULL; }
// clear the state of the array registry public function clear() { $this->_data = array(); } } There you have it. As you can see, creating a class that stores and fetches resources from an array is a painless task, one that you may have done many times before. In this case, this concrete registry not only implements the abstract methods declared by its corresponding parent, but it also inherits from it the “getInstance()” Singleton, thanks to the functionality of Late Static Bindings. That was really simple to grasp, wasn’t it? If you’re planning to build a registry that fits more specific requirements, the one shown above should be enhanced accordingly by adding to it some constraints, especially within its “set()” method. For instance, you might want to make the registry only suitable for storing/retrieving objects; in a case like this, make sure to include the appropriate validation code into this method, and everything should work just fine. Well, now that we have a concrete registry available for testing purposes, it’s time to give it a try and see if it really works as expected. So, in the next segment I’m going to set up an example that will show the registry’s functionality in a few simple steps. To learn more about how this example will be developed, jump forward and read the lines to come.
blog comments powered by Disqus |
|
|
|
|
|
|
|