ID:               45816
 User updated by:  gmtfn at yahoo dot com
 Reported By:      gmtfn at yahoo dot com
 Status:           Open
 Bug Type:         Session related
 Operating System: Linux
 PHP Version:      5.2.6
 New Comment:

line 25, not 26


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

[2008-08-14 07:37:54] gmtfn at yahoo dot com

Description:
------------
Calling serialize() on an object whose name is the same as the key
under which it's saved in $_SESSION makes the object unusable (its
methods can't be called.

If I store store a serialized version of the object under a key that's
different from the name of the variable that points to the object, then
everything works fine. If this is by design and must remain this way, I
want to tell you that this is VERY counterintuitive and must be
prominently documented.

I can reproduce this on a Linux server (with PHP 5.2.5 and 5.2.6) but
not on Windows Vista.

Reproduce code:
---------------
<?php
        session_start();
        ob_start();

        class GoodObject {
                private $count = 0;
                public function doSmth()
                {
                        echo "$this->count <br>";
                        $this->count++;
                }
        }

        if(!isset($_SESSION['myObj'])) {
                $myObj = new GoodObject();
        }
        else {
                $s = $_SESSION['myObj'];
                $myObj = unserialize($s);
        }
        
        $myObj->doSmth();
        $s = serialize($myObj);
        $_SESSION['myObj'] = $s;
        $myObj->doSmth(); // this fails on the second page load
?>

Expected result:
----------------
Converting an object to a string should not change the object, and the
last statement in this script should output the value of $count.

Actual result:
--------------
On the second load of the page, I get this:

Fatal error: Call to a member function doSmth() on a non-object in
[path to the file] on line 26


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


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

Reply via email to