Current location: Hot Scripts Forums » Programming Languages » PHP » $_SESSION Reference variable?


$_SESSION Reference variable?

Reply
  #1 (permalink)  
Old 04-11-08, 04:26 AM
dave111 dave111 is offline
Wannabe Coder
 
Join Date: Jul 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
$_SESSION Reference variable?

Does anyone know why reference variables with a session does not seem to work?
For example:

PHP Code:

class testing {

    var 
$sessvar;
    function 
testing(){
        
$this->sessvar =& $_SESSION['test'];
    }
    
    function 
doSomething{){
        
$this->sessvar 'Hello World';
    }

Why when doing "$this->sessvar = 'Hello Work';" does it not update the $_SESSION['test']?
Reply With Quote
  #2 (permalink)  
Old 04-11-08, 05:09 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
It's working for me.
PHP Code:

class testing {
    var 
$sessvar;
    function 
testing(){
        
$this->sessvar =& $_SESSION['test'];
    }
    
    function 
doSomething(){
        
$this->sessvar 'Hello World';
    }
}  


$test = new testing();

$test->doSomething();

echo 
$_SESSION['test']; 
... outputs "Hello World".
Reply With Quote
  #3 (permalink)  
Old 04-11-08, 05:44 AM
dave111 dave111 is offline
Wannabe Coder
 
Join Date: Jul 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Ok I've narrowed down the issue to this:
PHP Code:

class testing {

    var 
$sessvar;
    function 
testing(){
        
$this->sessvar =& $_SESSION['test'];
    }
    
    function 
doSomething(){
        unset(
$this->sessvar);  // <-- This line i want to clear out the $_SESSION['test'] but instead it appears to break the link between the variables, what should instead of "unset" to clear the session?
        
$this->sessvar 'Hello World';
    }
}  
echo 
$_SESSION['test']; 
Reply With Quote
  #4 (permalink)  
Old 04-11-08, 05:47 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
When unset()'ing references, the original variable maintains untouched. Do $this->sessvar = null; or something like that.
Reply With Quote
  #5 (permalink)  
Old 04-11-08, 05:47 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
if you pass on the variable as reference, you will always clear the original variable as well when you clear the new variable, that's the idea behind passing as reference

If you simply want to pass on the value of the $_SESSION['test'] variable, don't use the apmersand in front of $_SESSION['test']. Now you can clear the $_SESSION['test'] variable by using:
PHP Code:

unset ($_SESSION['test']); 

__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #6 (permalink)  
Old 04-11-08, 06:38 AM
dave111 dave111 is offline
Wannabe Coder
 
Join Date: Jul 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Nico View Post
When unset()'ing references, the original variable maintains untouched. Do $this->sessvar = null; or something like that.
Thank You.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading XML anupamsr PHP 13 04-19-08 12:57 PM
splitting variable output? vbsaltydog Perl 4 09-03-06 09:27 PM
Is there a variable cross reference or index utility jcap PHP 0 06-04-06 07:31 AM
templating problem (how to forward file name as a variable) skyrat PHP 0 09-11-05 08:27 PM
using variable variables with multiple variables for parts of the name harlock PHP 7 07-26-05 07:35 PM


All times are GMT -5. The time now is 07:42 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.