Page 1 of 1

Preserve Singleton object

Posted: Fri Jun 22, 2007 8:07 am
by superdezign
So I'm playing around with this Singleton pattern, and I ran into something that's messing me up.

I have objects that depend on Singleton pattern objects, so I define them like this:

Code: Select all

public function __construct()
{
    $this->__pObject = CObject::GetInstance();
}
However, if the instance in CObject changes, $__pObject does not. I'm considering sending the instance as a reference, but before that, I want to know if this is possible at all:

Code: Select all

private function Preserve()
{
    $this = self::$__instance;
}
I know it's not possible in the sense that I've written, but am curious as to whether or not there is a way to do this.

Posted: Fri Jun 22, 2007 8:11 am
by feyd
I'm still trying to understand why you need a singleton in the first place. Why not a registry?

Posted: Fri Jun 22, 2007 8:13 am
by superdezign
No idea. I haven't even read into it yet. Didn't sound like it was what I was after. I'll take a look. :D

Edit:
http://pattensforphp.com wrote:[...] a Registry can replace the need to have every object you need global access to acting as a Singleton.
:D:D:D Sounds like it's exactly what I want! Thanks feyd!

Posted: Fri Jun 22, 2007 8:37 am
by superdezign
Okay, so the registry will simplify a lot of this, but I think it still leaves me with the same problem of saving an object in the constructor, as each object may have a copy of the registry in a different state. Am I going to have to make the registry a singleton registry and access it through GetInstance() every time that I want to use it, or use a reference when passing it through constructors?

Posted: Fri Jun 22, 2007 8:40 am
by feyd
Many registries are built using statics and static calls.

Posted: Fri Jun 22, 2007 8:48 am
by superdezign
As in static functions with a static storage object? That never even came to mind. That sounds like it'd work very nicely.