php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #14266 serialize() doesn't work correcty
Submitted: 2001-11-28 08:32 UTC Modified: 2001-11-28 09:06 UTC
From: lenar at vision dot ee Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.2.0-dev OS: Debian Linux
Private report: No CVE-ID: None
 [2001-11-28 08:32 UTC] lenar at vision dot ee
Consider following simple script. It's pretty obvious
that serialize() does not work correctly (at least not as 
expected).

<?php
  $x = new stdClass();
  $x->x =& $x;
  $s = serialize($x) . "\n";

  echo "test on original: "; // OK
  $x->a = "str";
  echo $x->x->a . "\n";
  $o = unserialize($s);

  echo "test on unserialized object: "; // not OK
  $o->a = "str";
  echo $o->x->a . "\n"; 
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-11-28 08:38 UTC] jmoore@php.net
Circualar reference here:

$x->x =& $x;
  

These dont work as expected at all.

Bogus bug report as the lang isnt designed to support this.

- James
 [2001-11-28 08:38 UTC] lenar at vision dot ee
Tested this with current cvs too. No better luck.
Original and unserialized objects have different 
representation in memory.

 [2001-11-28 08:41 UTC] jmoore@php.net
Your creating a reference to itself this is not allowed.

Therefore the bug is bogus, the behaviour is undefined.

- James
 [2001-11-28 08:43 UTC] lenar at vision dot ee
I dont' agree. Circular refferences can happen with 
objects (using refferences). Sometimes they are 
intentional. And serialize() doesn't completely ignore 
this. Because it spits out:
O:8:"stdClass":1:{s:1:"x";O:8:"stdClass":1:{s:1:"x";R:2;}}

so it knows about circular refferences.

Now is there any reason why it doesn't generate instead 
something like this:

O:8:"stdClass":1:{s:1:"x";R:1;}

I would understand if it completely ignores circular 
references, but it doesn't. It just misbehaves.


 [2001-11-28 08:59 UTC] zimt@php.net
Behaviour of Circular / Selfreferencing objects is not defined. 

you could try to do a workarround 
by using defining __sleep() and __wakeup()
in your class, which are called when the object is serialized / unserialized, and could unset those self-references.

but you realy should do those.. if you dont destroy all of those references, you will get memory leaks!

regards, Peter Petermann
 [2001-11-28 09:01 UTC] zimt@php.net
sorry, i wanted to write:
you realy should *NOT*...
 [2001-11-28 09:06 UTC] lenar at vision dot ee
Ok, I tested a little bit more
and found that when calling serialize() like serialize(&$x)
it works.
I know about leaks, but those leaks don't kill me (yet).

And btw. Circular references work as expected. There is 
nothing so "undefined" about them in php. They work quite 
logically.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 06:01:32 2024 UTC