[Typo3-dev] singletons instead of pre-loaded objects
Robert Lemke
rl at robertlemke.de
Mon Aug 9 15:54:59 CEST 2004
On Mon, 2004-08-09 at 20:47, Martin T. Kutschker wrote:
> Why? You need some place to store the object references. As options I
> see either a global array, a master object known to be loaded or factory
> objects for every singleton (the object has a method with a static var
> as storage).
Because the recommended way for implementing the Singleton pattern is
not to store instantiation information in a global variable but use a
static variable within the class itself (at least that's what I remember
from the GOF book).
So what I meant is what you mentioned as option no. 3 (method with
static var), although I don't understand yet why it's called factory
objects.
But with you method, how would I access the object so it could be
created automatically?
> > function &makeInstance($className) {
> > $realClassName = t3lib_div::makeInstanceClassName($className);
> > if (function_exists ($realClassName.'::getInstance')) {
> > // Must be done this way, because call_user_func does not
> > // return references:
> > $tmpObj = call_user_func (array ($realClassName, 'getInstance));
> > return $tmpObj->getInstance();
> > } else {
> > new $realClassName;
> > }
> > }
> But this way you create every time a temp. object, so it's pointless.
> The point is to avoid object creation.
You mean the $tmpObj? It's true, this would be an unneccessary instance.
Does this work? :
if (function_exists ($realClassName.'::getInstance'))
return $realClassName::getInstance();
} else {
new $realClassName;
}
--
robert
"They placed me on this earth without a manual.
And I dare to say, I’m doing just fine without ;)"
More information about the TYPO3-dev
mailing list