php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36694 unserialize method is not called on an object when session is restored
Submitted: 2006-03-11 03:42 UTC Modified: 2008-11-10 01:00 UTC
Votes:9
Avg. Score:4.8 ± 0.6
Reproduced:8 of 8 (100.0%)
Same Version:7 (87.5%)
Same OS:6 (75.0%)
From: iain at iaindooley dot com Assigned: helly (profile)
Status: No Feedback Package: Class/Object related
PHP Version: 5.1.2 OS: FreeBSD 6.0
Private report: No CVE-ID: None
 [2006-03-11 03:42 UTC] iain at iaindooley dot com
Description:
------------
when an object is stored in the session, serialize is called on that object when the script finishes executing if that object implements Serializable, but unserialize is not called when the session is reloaded

Reproduce code:
---------------
<?
class SomeClass implements Serializable
{
    private $member;
    public $another;

    function SomeClass()
    {
        $this->member = 'member value';
        $this->another = 'another value';
    }

    public function serialize()
    {
        echo('called serialize<br />');
    }

    public function unserialize($serialized)
    {
        echo('called serialize<br />');
    }
}

class AnotherClass extends SomeClass
{
    function AnotherClass()
    {
        $this->SomeClass();
    }
}

$obj = new AnotherClass();
session_name('god');
session_start();
$_SESSION['var'] = $obj;
?>

Expected result:
----------------
called serialize
called unserialize

Actual result:
--------------
called serialize

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-11 03:44 UTC] iain at iaindooley dot com
that echo statement should be:

echo('called unserialize<br />');

in function unserialize(), still the same result though :-)
 [2008-09-11 13:24 UTC] kopelke at gmail dot com
Your code might be wrong.

serialize needs a return value:

<?
class [...] // your class

$obj = new AnotherClass();

echo $s_obj = serialize($obj);
// prints: N;
var_dump( unserialize( $s_obj ) );
// prints: NULL
?>

Your serialize implementation needs a string (or null) as a return value, it should serialize something. Without a return value it will automatically take null as a value hence returning nothing -> you get NULL.

Adding 'return "nothing";' to your serialize method and changing some code :-D

<?

class SomeClass implements Serializeable {
public function serialize()
    {
        echo('called serialize<br />');
return "Any String";
    }

    public function unserialize($serialized)
    {
        echo('called unserialize<br />');
    }
}

// thats all we need.

ob_start(); // for session and output
$obj = new SomeClass();
session_start(); // create Session
# on second run here will be written "unserialize"
$_SESSION['obj'] = $obj;
session_write_close(); // only to force it, will work without
# every run will write "serialize"

?>

Hope that helps, I don't think it's a Bug... But you might add a Exception or something if serialize returns NULL
I have not tested __wakeup and __sleep but I guess it's the same there.
 [2008-09-11 13:28 UTC] kopelke at gmail dot com
Hm.. I'm actually using XP, so I don't know if this is related anyhow.
Sorry if this is FreeBSD 6.0 specific.
 [2008-11-02 12:31 UTC] jani@php.net
Please try using this CVS snapshot:

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

  http://windows.php.net/snapshots/


 [2008-11-10 01:00 UTC] php-bugs at lists dot php dot net
No feedback was provided for this bug for over a week, so it is
being suspended automatically. If you are able to provide the
information that was originally requested, please do so and change
the status of the bug back to "Open".
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 22:01:27 2024 UTC