Professional Web Applications Themes

#25975 [Opn->Fbk]: PHP 5 object references don't survive serialization - PHP Development

ID: 25975 Updated by: [email]moriyoshiphp.net[/email] Reported By: reiersol at online dot no -Status: Open +Status: Feedback Bug Type: Zend Engine 2 problem Operating System: Linux RedHat 9.0 PHP Version: 5CVS-2003-10-24 (dev) New Comment: Please try using this CVS snapshot: [url]http://snaps.php.net/php5-latest.tar.gz[/url] For Windows: [url]http://snaps.php.net/win32/php5-win32-latest.zip[/url] This should be fixed in CVS now. Previous Comments: ------------------------------------------------------------------------ [2003-11-09 10:46:13] reiersol at online dot no Opening the bug again ------------------------------------------------------------------------ [2003-11-04 01:23:53] reiersol at online dot no object(foo)#1 (2) { ["v1"]=> object(bar)#2 (1) { ["value"]=> int(42) } ["v2"]=> object(bar)#2 (1) { ["value"]=> int(42) } } object(foo)#3 (2) { ["v1"]=> object(bar)#4 (1) { ["value"]=> string(42) "and ...

  1. #1

    Default #25975 [Opn->Fbk]: PHP 5 object references don't survive serialization

    ID: 25975
    Updated by: [email]moriyoshiphp.net[/email]
    Reported By: reiersol at online dot no
    -Status: Open
    +Status: Feedback
    Bug Type: Zend Engine 2 problem
    Operating System: Linux RedHat 9.0
    PHP Version: 5CVS-2003-10-24 (dev)
    New Comment:

    Please try using this CVS snapshot:

    [url]http://snaps.php.net/php5-latest.tar.gz[/url]

    For Windows:

    [url]http://snaps.php.net/win32/php5-win32-latest.zip[/url]

    This should be fixed in CVS now.



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

    [2003-11-09 10:46:13] reiersol at online dot no

    Opening the bug again

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

    [2003-11-04 01:23:53] reiersol at online dot no

    object(foo)#1 (2) {
    ["v1"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    ["v2"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    }
    object(foo)#3 (2) {
    ["v1"]=>
    object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    ["v2"]=>
    object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    }

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

    [2003-11-04 00:28:17] [email]sniperphp.net[/email]

    Add the missing expected result to your last example.




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

    [2003-10-31 03:42:04] reiersol at online dot no

    I guess I'll have to expand my example:

    class Bar { var $value = 0; }
    class Foo {
    var $v1;
    var $v2;
    function Foo() {
    $this->v1 = new Bar;
    $this->v2 = $this->v1;
    }
    }

    $f = new Foo;
    $f->v2->value = 42;
    var_dump($f);
    $g = unserialize(serialize($f));
    $g->v2->value = 'and now for something completely different';
    var_dump($g);

    Here's the output:

    object(foo)#1 (2) {
    ["v1"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    ["v2"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    }
    object(foo)#3 (2) {
    ["v1"]=>
    object(bar)#4 (1) {
    ["value"]=>
    int(42)
    }
    ["v2"]=>
    object(bar)#5 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    }

    That should at least make it clear that there's a difference in
    behavior before and after serialization. And the behavior before
    serialization is the behavior of a normal object-oriented language. (I
    ported the example to Java just to make sure I wasn't crazy.)

    I'm not trying to split hairs. I tried creating the kind of
    sophiticated object-oriented structure that PHP 5 makes so much easier.
    It worked wonderfully. But then I discovered that the structure didn't
    persist across sessions. So I made this simplified example to
    demonstrate the problem.

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

    [2003-10-24 08:08:59] reiersol at online dot no

    Description:
    ------------
    Object references inside PHP5 objects are not preserved through
    serialize/unserialize like traditional PHP4 references. This means they
    cannot be used in session-based applications.

    Reproduce code:
    ---------------
    class Bar {}
    class Foo {
    var $v1;
    var $v2;
    function Foo() {
    $this->v1 = new Bar;
    $this->v2 = $this->v1;
    }
    }

    $f = new Foo;
    var_dump($f);
    $g = unserialize(serialize($f));
    var_dump($g);
    print $s1;


    Expected result:
    ----------------
    This is what I get if I use $this->v2 = &this->$v1 instead of $this->v2
    = $this->v1:

    object(foo)#1 (2) {
    ["v1"]=>
    &object(bar)#2 (0) {
    }
    ["v2"]=>
    &object(bar)#2 (0) {
    }
    }
    object(foo)#3 (2) {
    ["v1"]=>
    &object(bar)#4 (0) {
    }
    ["v2"]=>
    &object(bar)#4 (0) {
    }
    }

    Actual result:
    --------------
    object(foo)#1 (2) {
    ["v1"]=>
    object(bar)#2 (0) {
    }
    ["v2"]=>
    object(bar)#2 (0) {
    }
    }
    object(foo)#3 (2) {
    ["v1"]=>
    object(bar)#4 (0) {
    }
    ["v2"]=>
    object(bar)#5 (0) {
    }
    }



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


    --
    Edit this bug report at [url]http://bugs.php.net/?id=25975&edit=1[/url]
    moriyoshi@php.net Guest

  2. #2

    Default #25975 [Opn->Fbk]: PHP 5 object references don't survive serialization

    ID: 25975
    Updated by: net
    Reported By: reiersol at online dot no
    -Status: Open
    +Status: Feedback
    Bug Type: Zend Engine 2 problem
    Operating System: Linux RedHat 9.0
    PHP Version: 5CVS-2003-10-24 (dev)
    New Comment:

    Please provide an example script that actually uses sessions,
    (it's not the same thing when you do the serialize/unserialize inside
    the same script..)




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

    [2003-11-10 05:48:59] reiersol at online dot no

    Thanks, but I'm afraid this is not quite good enough.

    What I'm getting now after serialize/unserialize is:

    object(foo)#3 (2) {
    ["v1"]=>
    &object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    ["v2"]=>
    &object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    }

    The ampersands occur only after serialization. They indicate that this
    is what is known as a reference in PHP 4, which is a symbol table
    alias. The objects still don't behave the same before and after. Try
    this:

    $f->v2 = 'I\'m no longer an object';
    $g->v2 = 'I\'m no longer an object';
    var_dump($f);
    var_dump($g);

    As before, $f is the object before serialize/unserialize, $g is the
    object after. The output is:

    object(foo)#1 (2) {
    ["v1"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    ["v2"]=>
    string(23) "I'm no longer an object"
    }
    object(foo)#3 (2) {
    ["v1"]=>
    &string(23) "I'm no longer an object"
    ["v2"]=>
    &string(23) "I'm no longer an object"
    }

    As I understand it, in $f we are replacing an object reference with a
    string. In $g, we are replacing the value of a variable that's aliased
    to another.

    You might ask whether this has any practical consequences. I think that
    sooner or later it will (in fact, it seems to be happening in my
    full-scale example). When it does, it will be very confusing to the
    people who encounter the problem. You may get away with this for a
    while, but the longer you get away with it the more difficult it might
    be to figure out.

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

    [2003-11-09 12:34:43] net

    Please try using this CVS snapshot:

    http://snaps.php.net/php5-latest.tar.gz

    For Windows:

    http://snaps.php.net/win32/php5-win32-latest.zip

    This should be fixed in CVS now.


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

    [2003-11-04 01:23:53] reiersol at online dot no

    object(foo)#1 (2) {
    ["v1"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    ["v2"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    }
    object(foo)#3 (2) {
    ["v1"]=>
    object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    ["v2"]=>
    object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    }

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

    [2003-11-04 00:28:17] net

    Add the missing expected result to your last example.




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

    [2003-10-31 03:42:04] reiersol at online dot no

    I guess I'll have to expand my example:

    class Bar { var $value = 0; }
    class Foo {
    var $v1;
    var $v2;
    function Foo() {
    $this->v1 = new Bar;
    $this->v2 = $this->v1;
    }
    }

    $f = new Foo;
    $f->v2->value = 42;
    var_dump($f);
    $g = unserialize(serialize($f));
    $g->v2->value = 'and now for something completely different';
    var_dump($g);

    Here's the output:

    object(foo)#1 (2) {
    ["v1"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    ["v2"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    }
    object(foo)#3 (2) {
    ["v1"]=>
    object(bar)#4 (1) {
    ["value"]=>
    int(42)
    }
    ["v2"]=>
    object(bar)#5 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    }

    That should at least make it clear that there's a difference in
    behavior before and after serialization. And the behavior before
    serialization is the behavior of a normal object-oriented language. (I
    ported the example to Java just to make sure I wasn't crazy.)

    I'm not trying to split hairs. I tried creating the kind of
    sophiticated object-oriented structure that PHP 5 makes so much easier.
    It worked wonderfully. But then I discovered that the structure didn't
    persist across sessions. So I made this simplified example to
    demonstrate the problem.

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

    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/25975

    --
    Edit this bug report at http://bugs.php.net/?id=25975&edit=1
    sniper@php.net Guest

  3. #3

    Default #25975 [Opn->Fbk]: PHP 5 object references don't survive serialization

    ID: 25975
    Updated by: net
    Reported By: reiersol at online dot no
    -Status: Open
    +Status: Feedback
    Bug Type: Zend Engine 2 problem
    Operating System: Linux RedHat 9.0
    PHP Version: 5CVS-2003-10-24 (dev)
    New Comment:

    Please try using this CVS snapshot:

    http://snaps.php.net/php5-latest.tar.gz

    For Windows:

    http://snaps.php.net/win32/php5-win32-latest.zip




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

    [2003-12-04 09:52:38] reiersol at online dot no

    OK. Here is an example using sessions. Exactly the same thing happens.


    <?php
    class Bar { public $value = 0; }
    class Foo {
    public $v1;
    public $v2;
    function Foo() {
    $this->v1 = new Bar;
    $this->v2 = $this->v1;
    }
    }
    session_start();

    if (isset($_SESSION['g'])) {
    //Try these two one at a time to see the different behaviors:
    // $_SESSION['g']->v2 = "I'm no longer an object";
    $_SESSION['g']->v2->value = 42;
    } else {
    $_SESSION['g'] = new Foo;
    }
    ?>
    <pre>
    <?php
    var_dump($_SESSION['g']);
    ?>
    </pre>

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

    [2003-11-28 20:40:06] net

    Please provide an example script that actually uses sessions,
    (it's not the same thing when you do the serialize/unserialize inside
    the same script..)



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

    [2003-11-10 05:48:59] reiersol at online dot no

    Thanks, but I'm afraid this is not quite good enough.

    What I'm getting now after serialize/unserialize is:

    object(foo)#3 (2) {
    ["v1"]=>
    &object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    ["v2"]=>
    &object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    }

    The ampersands occur only after serialization. They indicate that this
    is what is known as a reference in PHP 4, which is a symbol table
    alias. The objects still don't behave the same before and after. Try
    this:

    $f->v2 = 'I\'m no longer an object';
    $g->v2 = 'I\'m no longer an object';
    var_dump($f);
    var_dump($g);

    As before, $f is the object before serialize/unserialize, $g is the
    object after. The output is:

    object(foo)#1 (2) {
    ["v1"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    ["v2"]=>
    string(23) "I'm no longer an object"
    }
    object(foo)#3 (2) {
    ["v1"]=>
    &string(23) "I'm no longer an object"
    ["v2"]=>
    &string(23) "I'm no longer an object"
    }

    As I understand it, in $f we are replacing an object reference with a
    string. In $g, we are replacing the value of a variable that's aliased
    to another.

    You might ask whether this has any practical consequences. I think that
    sooner or later it will (in fact, it seems to be happening in my
    full-scale example). When it does, it will be very confusing to the
    people who encounter the problem. You may get away with this for a
    while, but the longer you get away with it the more difficult it might
    be to figure out.

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

    [2003-11-09 12:34:43] net

    Please try using this CVS snapshot:

    http://snaps.php.net/php5-latest.tar.gz

    For Windows:

    http://snaps.php.net/win32/php5-win32-latest.zip

    This should be fixed in CVS now.


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

    [2003-11-04 01:23:53] reiersol at online dot no

    object(foo)#1 (2) {
    ["v1"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    ["v2"]=>
    object(bar)#2 (1) {
    ["value"]=>
    int(42)
    }
    }
    object(foo)#3 (2) {
    ["v1"]=>
    object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    ["v2"]=>
    object(bar)#4 (1) {
    ["value"]=>
    string(42) "and now for something completely different"
    }
    }

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

    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/25975

    --
    Edit this bug report at http://bugs.php.net/?id=25975&edit=1
    sniper@php.net Guest

Similar Threads

  1. #25975 [Com]: PHP 5 object references don't survive serialization
    By cunha17 at uol dot com dot br in forum PHP Development
    Replies: 2
    Last Post: January 22nd, 01:22 AM
  2. #25975 [Bgs->Opn]: PHP 5 object references don't survive serialization
    By reiersol at online dot no in forum PHP Development
    Replies: 2
    Last Post: November 9th, 03:48 PM
  3. #25975 [Bgs]: PHP 5 object references don't survive serialization
    By sniper@php.net in forum PHP Development
    Replies: 1
    Last Post: November 4th, 06:25 AM
  4. Replies: 2
    Last Post: November 3rd, 11:44 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139