ID:               36694
 Comment by:       kopelke at gmail dot com
 Reported By:      iain at iaindooley dot com
 Status:           Assigned
 Bug Type:         Class/Object related
 Operating System: FreeBSD 6.0
 PHP Version:      5.1.2
 Assigned To:      helly
 New Comment:

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.


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

[2006-03-11 03:44:09] iain at iaindooley dot com

that echo statement should be:

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

in function unserialize(), still the same result though :-)

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

[2006-03-11 03:42:02] 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


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


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

Reply via email to