unserialize turns string into object ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LucosidE
Forum Newbie
Posts: 10
Joined: Fri Sep 25, 2009 9:56 am

unserialize turns string into object ??

Post by LucosidE »

can someone please explain this behavior ???

Code: Select all

echo "<br />BEFORE UNSERIALIZE: ".gettype($_SESSION['link']); 
$link = unserialize($_SESSION['link']);       
echo "<br />AFTER UNSERIALIZE: ".gettype($_SESSION['link']);
output :

BEFORE UNSERIALIZE: string
AFTER UNSERIALIZE: object

Why is my input string becoming an object ?? ? ? ?
Last edited by LucosidE on Mon Dec 14, 2009 2:52 pm, edited 1 time in total.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: unserialize ???

Post by requinix »

Code: Select all

$_SESSION["link"];
echo "<br />BEFORE UNSERIALIZE: ".gettype($_SESSION['link']);
$link = unserialize($_SESSION['link']);      
echo "<br />AFTER UNSERIALIZE: ".gettype($_SESSION['link']);
Now what happens?
LucosidE
Forum Newbie
Posts: 10
Joined: Fri Sep 25, 2009 9:56 am

Re: unserialize ???

Post by LucosidE »

same thing :(

BEFORE UNSERIALIZE: string
AFTER UNSERIALIZE: object
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: unserialize turns string into object ??

Post by AbraCadaver »

The type of serialized data is always a string, that's the point of serializing it!

If you serialize an object it will be a string. If you unserialize it you'll have your object back.

What did you expect?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
LucosidE
Forum Newbie
Posts: 10
Joined: Fri Sep 25, 2009 9:56 am

Re: unserialize turns string into object ??

Post by LucosidE »

i did not expect the string in session to become an object,

Code: Select all

 
$link = new link_obj(35545);
$_SESSION['link'] = serialize($link);
 
on another page i need to retrieve the object 
$link = unserialize($_SESSION['link']);
i dont see why $_SESSION['link']
is supposed to become an object here.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: unserialize turns string into object ??

Post by AbraCadaver »

LucosidE wrote:i did not expect the string in session to become an object,

Code: Select all

 
$link = new link_obj(35545);
$_SESSION['link'] = serialize($link);
 
on another page i need to retrieve the object 
$link = unserialize($_SESSION['link']);
i dont see why $_SESSION['link']
is supposed to become an object here.
I don't know what to tell you. This is exactly what serialize does and why people use it. In your code above:

2. create an object named $link
3. serialize this object and assign it to the session var 'link'
6. unserialize the session var 'link' and assign the object to the var $link
Last edited by AbraCadaver on Mon Dec 14, 2009 3:51 pm, edited 1 time in total.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: unserialize turns string into object ??

Post by AbraCadaver »

Actually, I missed that you are actually printing the type of the session var. That would make a difference. I get the expected results though (PHP 5.2.4):

Code: Select all

session_start();
$link = new stdClass;
 
$_SESSION['link'] = serialize($link);
echo "<br />BEFORE UNSERIALIZE: ".gettype($_SESSION['link']);
 
//on another page i need to retrieve the object
$link = unserialize($_SESSION['link']);
echo "<br />AFTER UNSERIALIZE: ".gettype($_SESSION['link']);
BEFORE UNSERIALIZE: string
AFTER UNSERIALIZE: string
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
LucosidE
Forum Newbie
Posts: 10
Joined: Fri Sep 25, 2009 9:56 am

Re: unserialize turns string into object ??

Post by LucosidE »

This doesn't make any sense at all :(
i think it randomly generates a bool and then based on
that makes it into a string or an object...

PHP Version 5.2.2

Any thoughts people ??
anyone had similar thing happen ?

I strongly dislike Netfirms now
LucosidE
Forum Newbie
Posts: 10
Joined: Fri Sep 25, 2009 9:56 am

Re: unserialize turns string into object ??

Post by LucosidE »

Last night :
:banghead:
give up
:drunk:

today morning
:? :D :?: :?: :?:

I guess the server needed to sleep on it.
Everything works fine today morning

I am still very confused as to why it didn't work
the first place and does now ...

I was using NuSphere to develop and test
and everything was perfect, uploaded to
Netfirms, session was behaving weird and sometimes
perfectly fine mysql queries give errors. seems ok now ...
Post Reply