Page 1 of 1

Passing Session Object by reference

Posted: Sun Jan 11, 2009 1:47 am
by msurabbott
~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.


So I have the following code:

Code: Select all

$authenticatedUser = unserialize($_SESSION['authenticatedUser']);
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $authenticatedUser->location);
Where the updateLocation method takes a second parameter by reference... and in the method:

Code: Select all

public function updateLocation($data, &$locationObj = null) {
// Confirm that the object being updated is of the correct type, if provided
if($locationObj != null && !is_a($locationObj, 'Location')) {
    return false;
}
        
$sql = "UPDATE $this->table " . 
    "SET add1 = '". $data['add1'] ."', " . 
    "add2 = '". $data['add2'] ."', " . 
    "city = '". $data['city'] ."', " . 
    "state_id = ". $data['state_id'] .", " . 
    "zip = '". $data['zip'] ."' " . 
    "WHERE id = " . (($locationObj != null) ? $locationObj->id : $data['id']);
        
$result = $this->db->query($sql);
        
if(mysql_affected_rows($this->db->conn)) {
    if($locationObj != null) {
        $locationObj->populate($data);
    }
            
    return true;    
}
else {
    return false;
    }
}
it repopulates the objects data ('by reference') and I was thinking that it would modify the values, but it doesn't - since its an object in the session I have to log out, and back in for it to show up as changed.. any ideas as to why?

Thanks in advance!


~pickle | Please use [ code=html ], [ code=php ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: :arrow: Posting Code in the Forums to learn how to do it too.

Re: Passing Session Object by reference

Posted: Sun Jan 11, 2009 3:05 am
by requinix
99% likely:

$authenticatedUser is being modified, but you have to save it back into $_SESSION.

Code: Select all

$authenticatedUser = unserialize($_SESSION['authenticatedUser']);
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $authenticatedUser->location);
$_SESSION['authenticatedUser'] = serialize($authenticatedUser);
Alternatively, assign $authenticatedUser by reference:

By the way, stuff in $_SESSION is automatically serialized. Meaning you don't need to use serialize/unserialize:

Code: Select all

$authenticatedUser = $_SESSION['authenticatedUser'];
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $authenticatedUser->location);
$_SESSION['authenticatedUser'] = $authenticatedUser;
 
// but since $authenticatedUser is a copy of another variable 
$authenticatedUser =& $_SESSION['authenticatedUser']; // by reference
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $authenticatedUser->location);
// no saving necessary
 
// but that's still more work than necessary: just use $_SESSION[authenticatedUser] directly
$locationDS = new LocationDataSource();
$locationDS->updateLocation($_POST, $_SESSION['authenticatedUser']->location);

Re: Passing Session Object by reference

Posted: Sun Jan 11, 2009 10:03 am
by msurabbott
tasairis I tried your last solution before I posted, and found that the unserialize was necessary.. if I do what you suggested last I get null back for the location object, where as if I unserialize it I get the object just fine (so its not that the object inst loaded properly).

This was the case with all objects in the session, location is an object, of the user object - so because of this I added the serialize/unserialize and things began to work.

I have also tried the solution second on your list with no luck.. nothing seems to be saving, or updating by reference correctly..

Re: Passing Session Object by reference

Posted: Tue Jan 13, 2009 9:33 pm
by msurabbott
I really need help on this.. anyone else out there think they could provide a little assistance?

Re: Passing Session Object by reference

Posted: Tue Jan 13, 2009 9:59 pm
by requinix
Huh, maybe the serialization thing is new to PHP 5.

Just to make sure, you tried the first solution I gave, right?

Re: Passing Session Object by reference

Posted: Wed Jan 21, 2009 12:42 pm
by msurabbott
Yes, I tried them all with no success.. I am currently having to reload the user object every time the page loads, and reassign to the session obj.. which is obviously not desirable.