[Typo3-dev] singletons instead of pre-loaded objects
Martin T. Kutschker
Martin-no5pam-Kutschker at blackbox.n0spam.net
Mon Aug 9 18:16:43 CEST 2004
Hi!
How about adding t3lib_div::getSingleton or changing t3lib::getInstance
to observe class::is_singleton().
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.
Of course this means that the singleton must not require any startup
code that cannot be run in its constructor! Otherwise all of the
accessing routines would have to call them, which is error prone and an
overhead.
I have stumbled across this beacause I was writign a class that will run
in BE and FE mode but needed t3lib_cs. If I don't want to create an
additional instance of t3lib_cs I have to detect the TYPO3 mode and then
get to the object differently. All this wouldn't be necessary if we had
a singleton API in place.
IMHO this allows for simple extension of APIs and introducing of new
classes. Eg instead of bloating t3lib_cs with rarely used code we could
have a t3lib_csutils and need not to worry about where to instantiate
it. If any code needs it, it will get loaded - just once!
Masi
More information about the TYPO3-dev
mailing list