<?php
class Auth {
static public function authenticate($user, $passwd) {
//.......
if($result) {
$_SESSION['auth'] = serialize($userObject);
return true;
}
return false;
}
static public function &getAuthUser()
{
return unserialize($_SESSION['auth']);
}
}
Auth::authenticate();
$userObject = &Auth::getAuthUser();
$userObject->name = "test";
echo $userObject->name; //test
echo $_SESSION['auth']->name; //original name
?>
Right now I have to manually update the SESSION each time I make a change to the user object so that the change is also reflected in the SESSION.
And it seems like the pseudo code won’t work due to the unserialize() which makes sense…
I would like any changes to the $userObject to be reflected in the SESSION automatically.
I’m thinking this may not be possible because of the explicit serialize/unserialize, but I’d thought I’d throw it out there just in case.
So any suggestions are welcomed.
Thanks!
Notes:
I have to explicitly call serialize and unserialize because it seems like in php 5.1 (can’t upgrade), the __sleep and __wakeup magic functions aren’t invoked automatically when setting an obj to a SESSION var. In 5.2, everything seems to work just fine.