|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 24 08:00:02 2025 UTC |
that echo statement should be: echo('called unserialize<br />'); in function unserialize(), still the same result though :-)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.