ID:               33586
 Updated by:       [EMAIL PROTECTED]
 Reported By:      wmeler at wp dot pl
-Status:           Assigned
+Status:           Bogus
 Bug Type:         Scripting Engine problem
 Operating System: *
 PHP Version:      5CVS-2005-07-06
 Assigned To:      dmitry
 New Comment:

serialize() 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;
?>




Previous Comments:
------------------------------------------------------------------------

[2005-11-07 02:27:49] jfranklin at gmail dot com

I apologize for "cluttering the database" as you say, but this would be
a super bug to put some time into and get fixed.

------------------------------------------------------------------------

[2005-10-07 04:36:42] james at gogo dot co dot nz

This has always been a problem, at least in 4.x (I presume it still is
the same), I wrote an article about it a couple of years back now.

http://code.gogo.co.nz/dev-2-dev/serialize-unserialize.html

The workaround that worked at the time at least is to force passing in
a reference:
  $serializedFoo = serialize(&$myFoo);

------------------------------------------------------------------------

[2005-09-02 18:28:07] sr at brightlight dot ch

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

regards

------------------------------------------------------------------------

[2005-07-22 08:52:12] wmeler at wp dot pl

Do you want simplest to debug example or complicated real world
example?

How about this :

<?
$c = array();
$d = array();

$c['d2']=&$d;
$d['c2']=&$c;
$x=unserialize(serialize($c));
$x['x']='x';
$c['x']='x';
var_dump($c);
var_dump($x);
?>

outputs remain the same

you can even substitute 'c' with 'parent' and 'd' with 'child' which
makes it more real but this would change outputs

------------------------------------------------------------------------

[2005-07-22 01:03:09] [EMAIL PROTECTED]

In your example you're making a reference to a non-existing variable.
Please come up with something more realistic.


------------------------------------------------------------------------

The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
    http://bugs.php.net/33586

-- 
Edit this bug report at http://bugs.php.net/?id=33586&edit=1

Reply via email to