[Typo3-dev] singletons again

Martin T. Kutschker Martin-no5pam-Kutschker at blackbox.n0spam.net
Thu Aug 19 17:24:13 CEST 2004


Hi!

I cannot remember any decision on this subject, so I repost my last 
suggestion:

class singleton    {

   // this function has to be in every class that is a singleton
   function &getInstance(&$ref)    {
     static $obj = null;

     if (!is_object($obj))    { $obj = new singleton(); }
              // pass back in argument for call_user_func()
     $ref->instance =& $obj;

     return $obj;
   }
}

class t3lib_div {

   // this is the changed makeInstance
   function &makeInstance($className)    {
     $realClassName =  t3lib_div::makeInstanceClassName($className);
     if (is_callable(array($realClassName, 'getInstance')) {
       call_user_func(array($realClassName, 'getInstance'),&$tmpObj);
       return $tmpObj->instance;
     } else {
       new $realClassName;
     }
   }
}

I further suggest that getInstance() is implemented for all extising 
singletons. The preloaded objects in global vars or properties of other 
objects would stay. I see having both way to access these classes as a 
convenience.

Masi





More information about the TYPO3-dev mailing list