|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-07-06 12:03 UTC] wmeler at wp dot pl
Description:
------------
After serializing variable with circular reference you get 2 copies of root variable.
Look at output of reproduce code - var_dump outputs should be the same but they are not.
for code:
$c['d2']=&$d;
$d['c2']=&$c;
echo serialize($c);
you get
a:1:{s:2:"d2";a:1:{s:2:"c2";a:1:{s:2:"d2";R:2;}}}
I think that it should be something like
a:1:{s:2:"d2";a:1:{s:2:"c2";R:1;}}
Reproduce code:
---------------
<?
$c['d2']=&$d;
$d['c2']=&$c;
$x=unserialize(serialize($c));
$x['x']='x';
$c['x']='x';
var_dump($c);
var_dump($x);
Expected result:
----------------
array(2) {
["d2"]=>
&array(1) {
["c2"]=>
&array(2) {
["d2"]=>
&array(1) {
["c2"]=>
&array(2) {
["d2"]=>
*RECURSION*
["x"]=>
string(1) "x"
}
}
["x"]=>
string(1) "x"
}
}
["x"]=>
string(1) "x"
}
array(2) {
["d2"]=>
&array(1) {
["c2"]=>
&array(2) {
["d2"]=>
&array(1) {
["c2"]=>
&array(2) {
["d2"]=>
*RECURSION*
["x"]=>
string(1) "x"
}
}
["x"]=>
string(1) "x"
}
}
["x"]=>
string(1) "x"
}
Actual result:
--------------
array(2) {
["d2"]=>
&array(1) {
["c2"]=>
&array(2) {
["d2"]=>
&array(1) {
["c2"]=>
&array(2) {
["d2"]=>
*RECURSION*
["x"]=>
string(1) "x"
}
}
["x"]=>
string(1) "x"
}
}
["x"]=>
string(1) "x"
}
array(2) {
["d2"]=>
&array(1) {
["c2"]=>
array(1) {
["d2"]=>
&array(1) {
["c2"]=>
array(1) {
["d2"]=>
*RECURSION*
}
}
}
}
["x"]=>
string(1) "x"
}
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 04 19:00:02 2025 UTC |
I experienced a similar problem. An even simpler setup already breaks unserialisation (php 5.0.4): $rec = array('rec' => 'x'); $rec['rec'] = &$rec; echo "print_r:\n".print_r($rec, true); echo "\nafter unserialisation:\n".print_r(unserialize (serialize($rec)), true); The output will be: print_r: Array ( [rec] => Array *RECURSION* ) after unserialisation: Array ( [rec] => Array ( [rec] => ) ) With a few more dimensions before the recursion php will even crash on OS X 10.4.1 regardsserialize() accepts argument by value and unserialize() returns by value so these function cannot be used directly to serialize/deserialize references. However it is possible do it using additional top-level container. The following exaple produces output that you are expecting. <?php function serialize_ref(&$c) { return serialize(array(&$c)); } function &unserialize_ref($s) { $ar = unserialize($s); return $ar[0]; } $c=array(); $d=array(); $c['d2']=&$d; $d['c2']=&$c; $x=&unserialize_ref(serialize_ref($c)); $x['x']='x'; $c['x']='x'; var_dump($c); var_dump($x); /* remove circular dependencies to avoid memory leaks */ $c['d2'] = null; $x['d2'] = null; ?>