[TYPO3-ect] div/lib extensions

Ernesto Baschny [cron IT] ernst at cron-it.de
Tue May 2 20:19:02 CEST 2006


Elmar Hinz schrieb am 02.05.2006 18:48:

> Well, I think I am not really the right object for fan-atism. ;-) I live
> fine with my own self-respect.

True, but your work is very good here, so this was is a way to motivate
you to keep up the nice work.

>> Other than that I needed an easy way of generating singletons of some of
>> my classes. The easiest and most generic method I found is the one
>> described in [1]: a helper class. It would be cool to have such a helper
>> as tx_div::getSingleton($className) which could also use the fancy
>> "class finder" routines (load et all). Something like:
>> 
>>     /**
>>      * Static method to get a singleton
>>      *
>>      * @param   string  classname 'tx_key_path_file'
>>      * @param   mixed   loadertype 't3', 'pear', 'classPear', 'hippie'
>> resp. 2, 3, 6, 9, 68
>>      * @return  object  the instance else FALSE
>>      */
>>     function getSingleton($className, $type = 3) {
>>         static $instances = array();  // array of instance names

> How about static $instances and PHP4? Never heard of static in PHP4. But
> could be replaced with a global if not available there.

Static variables in functions have been around since the start of PHP4.
But what I just found out, that references (to objects) in static
variables doesn't work as expected, see last example in
http://www.php.net/manual/en/language.variables.scope.php.

In my quick tests, this seems to work:

    function &getSingleton($className, $type = 3) {
        static $instances = array();  // array of instance names
        $className = strtolower($className);
        if (!array_key_exists($className, $instances)) {
            // instance does not exist, so create it
            $instances[$className] = tx_div::makeInstance($className,
$type);
        }
        return $instances[$className];
    }



Cheers,
Ernesto



More information about the TYPO3-team-extension-coordination mailing list