Re: Issue with the casting of a SESSION variable



On Oct 29, 1:08 pm, Suhas Dhoke <suhasdh...@xxxxxxxxx> wrote:
On Oct 29, 4:00 pm, AndreH <aha...@xxxxxxxxx> wrote:



Good day,

I have the a bit of an issue with retrieving an object from php's
session.

I set a session variable "user" from the class User as follows:
$user = new User();
// ... do some stuff to $user
$_SESSION['user'] = serialize($user);

And try to retrieve the variable again later:
$user=(User)unserialize($_SESSION['user']);

But the last statement throws this error:
Parse error:  syntax error, unexpected T_STRING in ...

Google did not produce any useful solutions to this problem; Other
than syntax issues, but I'm 100% sure it is not an earlier syntax
issue. The problem lies in the casting of session object.

Any help would be appreciated.

Regards,
Andre

Hello Andre.

You are trying (type-casting) while retrieving the details from
session to the variable.
There is no such type "User".
and also no need to add that.
Try without "(User)".

Thanks for the reply.

I removed the "(User)" part as follows:
$user=unserialize($_SESSION['user']);

But get an exception when I call one of its functions:

if($user->getSuperUser()) // Do something...

Here is the exception:
Fatal error: Call to a member function getSuperUser()

It works if I go:
$user = new User();
if($user->getSuperUser()) // Do something...

Here's my User class:
class User
{
var $ID = 0;
/* Some more members ... */
var $SuperUser = false;

function setValues($ID, /* Some more members ... */
$SuperUser)
{
$this->ID = $ID;
/* Some more members ... */
$this->SuperUser = $SuperUser;
}

function getSuperUser()
{
return $this->SuperUser;
}
}
.



Relevant Pages