Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
class Registry
{
var $_cache;
function Registry() {
$this->_cache = array();
}
function setEntry($key, &$item) {
$this->_cache[$key] = &$item;
}
function &getEntry($key) {
return $this->_cache[$key];
}
function isEntry($key) {
return ($this->getEntry($key) !== null);
}
function &instance() {
static $registry;
if (!$registry) {
$registry = new Registry();
}
return $registry;
}
}
Can anyone point me in the right direction on how to maintain a single instance of any particular class through my whole application?
Lets use my httpRequest class as an example.. from what I've gotten out of it all I have to do is extend the singleton and call $this->singleton() in the constructor to input the instance into cache.. apparantly that's not right?
ahh that makes things much clearer.. for some reason I though the class name was given through get_class() but that doesn't make sense the way it is setup. Thats what you get for theorizing
Just realized, at what point does the singleton come into play...
class whatever{
function whatever(){
$registry = &Registry::instance();
$mog = &Singleton::instance('mog');
$mog->mogstr('I am a mog part man, part dog.');
echo $mog -> mogstr;
}
}
FYI.. no need to set $registry instance in your example since the static call to instance in Singleton does it for you[/img]
Interesting. I'm sure there is more for me to discover about this very useful pattern. Anybody who has worked with OOP before in PHP knows what a pain it is to have to pass objects around by reference or create new ones in classes. This is so much easier!