Andrey Hristov php@hristov.com writes:
There are absolutely ways to do this purely at the application level.
It just
seems that it'd be a really clean, easy to program, easy to understand
methodology to pass the key value to the request for a new instance, and
get
back either a new instance, or a previously existing one with that key
value.
I don't think it's "really clean" to not return a new instance with a
new call. I'm not sure what kind of advantage it gives you over a
private constructor and a static getInstance($n) function.
Cheers,
Michael
cm@leetspeak.org writes:
I don't think it's "really clean" to not return a new instance with a
new call. I'm not sure what kind of advantage it gives you over a private
constructor and a static getInstance($n) function.
"Really clean" in that I believe that it's the class' business what it returns.
If the class wants to be a singleton, the user of the class doesn't even need
to know it is getting the same object returned each time it asks for a "new"
one. If the class wants to return a pre-existing object in some cases but a
newly-allocated object in other cases, I believe that's up to the class, not
to the caller. That is a characteristic of the design of the class. (I can
envision alternative viewpoints, however. :-)
Given my viewpoint, letting the caller request a "new" object each time (when
in fact the class intentionally -- sometimes or always -- returns a pre-
existing object because that's the design of the class), seems clean.
Two responses, two negatives. Ok, I withdraw the request. :-) I will not
belabor the point further, but will be glad to discuss the merits of this and
its implementation if any one else feels it could be a useful feature.
Cheers,
Derrell
but will be glad to discuss the merits of this and
its implementation if any one else feels it could be a useful feature.
Hi Derrell,
Below is some code I wrote to do something very similar to this from
an external perspective. It basically replaces the use of $this with a
reference to the class's own getInstance() method. The getInstance is
not used in code which calls the class, so the fact that it is all
running off the same instance is not visible from outside.
I'm not sure what the memory usage looks like if you do things like
this. I think the key would be to put __constructor code in the
getInstance mathod instead. A clean way to do this internally would be
nice. A way to redefine $this may be better than a __new() method.
Some sort of method_preview would be good enough for the code belown
(where $this would be set to self::instance()).
class StaticClass {
//// Statics Code ////
static protected $instance = false;
protected function instance() {
if(!self::$instance) {
self::$instance = new self;
}
return self::$instance;
}
//// Class Code ////
protected $value;
function set_value($value) {
$t = self::instance(); // Call this whenever you want a private variable
$t->value = $value;
}
function get_value() {
$t = self::instance();
return $t->value;
}
}
Regards,
Douglas