Re: I18N_DateTime unsing static instance

From: Date: Wed, 21 May 2003 14:20:42 +0000
Subject: Re: I18N_DateTime unsing static instance
References: 1 2 3 4  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-16580@lists.php.net to get a copy of this message
Jan Schneider wrote:
yeah sounds cool too. so you would always have to carry the LOCALE-varibale/constant around in the code? (it's now represented by 'de_DE') or should it work like this: very first call:
    $dateTime = &I18N_DateTime::singleton('de_DE');
every other call would/could be:
    $dateTime = &I18N_DateTime::singleton();
in the second call you wouldnt need the LOCALE, since the singleton was 'initialized' before
That doesn't quite follow the singletno pattern, but actually it depends how you code the singleton() method. I'd advise you to use the locale everytime though, because the whole purpose of the singleton is that you always get an object instance back without having to care about the actual instantiation.
i understand your point. and it might not be correct from the pattern point of view. but if i have to pass the LOCALE everytime, then i (imho) have not won much, except for having a singleton method. Because i would have to do it like this, when my LOCALE is saved in a varibale:
    //somewhere in the global namespace
    $locale = getLanguageShowhow()
    function any()
    {
        global $locale;
        $dateTime =& I18N_DateTime::singleton($locale);
        $dateTime->format($timestamp);
    }
if the locale was set in a constant, then it would not need the global-definition. but in my eyes the locale has to be retreived somehow and therefore 'belongs' in a variable. So it would be shorter (may be not better) but handier, if i use the $dateTime object from the global namespace and do it like this:
    //somewhere in the global namespace
    $locale = getLanguageShowhow()
    $dateTime =& I18N_DateTime::singleton($locale);
    function any()
    {
        global $dateTime;
        $dateTime->format($timestamp);
    }
or if i (ab)used the singleton pattern, the way mentioned before.
    //somewhere in the global namespace
    $locale = getLanguageShowhow()
    // setup the object, so the next singleton() call 'knows'
    // the locale it works on
    I18N_DateTime::singleton($locale);
    function any()
    {
        $dateTime =& I18N_DateTime::singleton($locale);
        $dateTime->format($timestamp);
    }
i understand your concern, about abusing the singleton. i would just try to find the 'best' way to do it. i like the last one best, even though it has the overhead of calling the singleton() every time. looks cleaner (but may be not clean enough :-)) Thank you for your input. -- Wolfram ... opensource @ vision:produktion ... http://opensource.visionp.de ... translating template engine .... http://sf.net/projects/simpletpl ... authentication system .... http://sf.net/projects/auth

« previous php.pear.dev (#16580) next »