TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Singleton in Session variable? (http://www.talkphp.com/absolute-beginners/5004-singleton-session-variable.html)

svdelle 10-06-2009 01:37 PM

Singleton in Session variable?
 
How can I store a singleton in a session variable, and only change properties if $_GET['lang'] is set?

Tanax 10-06-2009 05:42 PM

php Code:
$singletonVar = Factory::getInstance();
$_SESSION['var'] = $singletonVar;

if(isset($_GET['lang']))
{

      // Change your properties of $_SESSION['var'] here.

}

??

svdelle 10-06-2009 08:36 PM

Thanks, I'll check it out.

svdelle 10-07-2009 10:41 AM

Nope, variable still gets reset on every request. The singleton gets reinstanciated every freakin' time on the roundtrip to the server.

Simply cannot see how the singleton design pattern is of ANY use in a 'web language' as PHP.

sketchMedia 10-07-2009 01:57 PM

I think there is a fundamental misunderstanding of this pattern in PHP and its many uses.

A singleton is a class where only one object can created from it, thus making the object of that type a 'singleton' (as its the only one allowed to be created).

This allows you to make sure that only one version of that object is alive at that particular execution, of course it will have to be created again when the next request happens.

In the example given above, the session variable will be over-written on each execution because you don't make the check to see if it already exists, so every request it will be over-written.

Something like this will work:
PHP Code:


class SingletonClassName
{
    private static 
$_pSelf NULL;

    private function 
__construct() {}
    private function 
__clone() {}
    
    public static function 
getInstance()
    {   
        if(!
self::$_pSelf instanceof self)
        {   
            
//is it in the session
            
if(isset($_SESSION['var']) && $_SESSION['var'] instanceof self)
            {   
                return 
self::$_pSelf $_SESSION['var'];
            }

            
//set the self reference
            
self::$_pSelf = new self;

            
//set it up in the session
            
$_SESSION['var'] = self::$_pSelf ;

            
//return the object
            
return self::$_pSelf;
        }   
        return 
self::$_pSelf;
    }   
}

$class SingletonClassName::getInstance(); 

That will now, create a session variable and check on each page view for that object before creating a new one, thus your singleton is now always the same (well until the session expires).

I still don't quite get why you would need this though, most of my singletons are only for that execution and any variables i need saving between page views just go into a namespace'd session via my session wrapper, I very rarely need to save objects to a session because of the way my system works.

ioan1k 10-07-2009 02:05 PM

I wouldn't recommend storing the singleton itself within the global session space as this will eventually lead to excessive server loading with all of the singleton data ....

The easier thing to do here is store only the configuration you are using for the Singleton and then recreate it on the next page load

If this is impossible as you are storing to much information in your singleton, then you need to rethink your code structure as it is flawed because you are relying heavily on data that may not be 100% accurate

sketchMedia 10-07-2009 02:19 PM

I agree, like I said in my post, I have rarely if ever needed to store a full object into a session variable.


All times are GMT. The time now is 11:07 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0