TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-06-2009, 01:37 PM   #1 (permalink)
The Wanderer
 
Join Date: Oct 2009
Posts: 5
Thanks: 0
svdelle is on a distinguished road
Default Singleton in Session variable?

How can I store a singleton in a session variable, and only change properties if $_GET['lang'] is set?
svdelle is offline  
Reply With Quote
Old 10-06-2009, 05:42 PM   #2 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

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

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

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

}

??
__________________
Tanax is offline  
Reply With Quote
Old 10-06-2009, 08:36 PM   #3 (permalink)
The Wanderer
 
Join Date: Oct 2009
Posts: 5
Thanks: 0
svdelle is on a distinguished road
Default

Thanks, I'll check it out.
svdelle is offline  
Reply With Quote
Old 10-07-2009, 10:41 AM   #4 (permalink)
The Wanderer
 
Join Date: Oct 2009
Posts: 5
Thanks: 0
svdelle is on a distinguished road
Default

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.
svdelle is offline  
Reply With Quote
Old 10-07-2009, 01:57 PM   #5 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

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.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Old 10-07-2009, 02:05 PM   #6 (permalink)
The Contributor
 
ioan1k's Avatar
 
Join Date: Mar 2009
Location: US
Posts: 76
Thanks: 0
ioan1k is on a distinguished road
Default

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
__________________
My Portfolio - Work - Need freelance Work?
I've been developing 5 years now, and I learn something new everyday
ioan1k is offline  
Reply With Quote
Old 10-07-2009, 02:19 PM   #7 (permalink)
The Prestige
Advanced Programmer Top Contributor Good Samaritan 
 
sketchMedia's Avatar
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 854
Thanks: 32
sketchMedia is on a distinguished road
Default

I agree, like I said in my post, I have rarely if ever needed to store a full object into a session variable.
__________________
mysql> SELECT * FROM `users` WHERE `users`.`clue` > 0;
Empty set (0.00 sec)
sketchMedia is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Tip] Variable Prefixes bluesaga Tips & Tricks 24 10-22-2012 09:40 AM
Huge Session Problem Killswitch General 1 11-17-2008 02:36 AM
A Generic Singleton Base Class Theo Advanced PHP Programming 7 08-18-2008 02:25 AM
What is a Variable? codefreek Absolute Beginners 7 07-01-2008 08:29 PM
Understanding the Life of a Session Wildhoney General 6 10-27-2007 02:34 AM


All times are GMT. The time now is 10:04 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design