[Typo3-dev] singletons instead of pre-loaded objects
Robert Lemke
rl at robertlemke.de
Mon Aug 9 14:02:35 CEST 2004
Hi Martin,
On Mon, 2004-08-09 at 18:16, Martin T. Kutschker wrote:
> The idea is that in a global var we have an array that has references to
> all classes that need only one instantiation, eg t3lib_db and t3lib_cs.
> Instead of accessing them via a well known global var (eg t3lib_db) or
> some pre-load mechanism (eg t3lib_cs) the code would simply call the
> stattic method of tr3lib_div and will get a reference to the object. The
> code in t3lib_div will either create a new object or pass the reference
> of an existing one.
okay, but I think that generally it is not a good idea to put
instantiation information about singletons into some global array and
access these functions like you suggested.
How about this: Every class which can be instantiated as a singleton
(like t3lib_cs) must (or may) provide a method called "getInstance".
Then t3lib_div::makeInstance could look like this (didn't check it, may
be done differently):
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;
}
}
This would be 100% backwards-compatible and future code could take
advantage of the singleton mechanism.
--
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