By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,830 Members | 2,271 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,830 IT Pros & Developers. It's quick & easy.

Issue with the casting of a SESSION variable

P: n/a
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
Oct 29 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Oct 29, 4:00*pm, AndreH <aha...@gmail.comwrote:
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)".
Oct 29 '08 #2

P: n/a
On Oct 29, 1:08*pm, Suhas Dhoke <suhasdh...@gmail.comwrote:
On Oct 29, 4:00*pm, AndreH <aha...@gmail.comwrote:
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;
}
}
Oct 29 '08 #3

P: n/a
On Oct 29, 1:37*pm, AndreH <aha...@gmail.comwrote:
On Oct 29, 1:08*pm, Suhas Dhoke <suhasdh...@gmail.comwrote:
On Oct 29, 4:00*pm, AndreH <aha...@gmail.comwrote:
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;
* * * * }
* * }
Ok found my problem.

Somehow the $_SESSION["user"] variable is set to the boolean value
"false", even though this assignment is never made. So my program
tries to access the SuperUser method, which obviously doesn't exist
for a boolean.

Thanks for the help Suhas.
Oct 29 '08 #4

P: n/a
AndreH 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']);
Why? A session already serializes its values when stored, no need to do
it twice. You can perfectly store an object in a session, as long as
you've loaded the class definitions before session_start().

Keep in mind not every object can be serialized, see the manual:
http://nl2.php.net/serialize
"serialize() handles all types, except the resource-type. You can even
serialize() arrays that contain references to itself. Circular
references inside the array/object you are serialize()ing will also be
stored. Any other reference will be lost."

Something not serializable is by default not storable in a session,
allthough you could employ the __sleep() & __wakeup() methods to remove
any unserializable content, and recreate them on unserialize.
--
Rik
Oct 29 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.