MENU
Serialization
An object can be serialized to a string (and stored into a file). As serialize($o) is called, PHP first runs the magic function __sleep(), which returns an array of the variable names to be serialized.To restore a serialized string to an object, use unserialize($s). __wakeup() will be called after an object has been unserialized.
<!DOCTYPE html>
<html>
<head></head>
<body><?php
class MyClass{
public $a;
public $b;
public $c;
public $arr=['hello','world'];
function __sleep(){
return ['a','b','arr'];
}
function __wakeup(){
$this->c=50000;
}
}
$o=new MyClass;
$o->a=100;
$o->b=2000;
$o->c=30000;
$s=serialize($o);
echo $s;
$o2=unserialize($s);
echo "<br />$o2->a";
echo "<br />$o2->b";
echo "<br />$o2->c";
?></body>
</html>
O:7:"MyClass":3:{s:1:"a";i:100;s:1:"b";i:2000;s:3:"arr";a:2:{i:0;s:5:"hello";i:1;s:5:"world";}} 100 2000 50000
RESETRUNFULL
<!DOCTYPE html>
<html>
<head></head>
<body><?php
class A{
public $v1;
public $v2;
public static function __set_state($arr) {
$obj = new A;
$obj->v1 = $arr['v1'];
$obj->v2 = $arr['v2'];
return $obj;
}
}
$a = new A;
$a->v1 = 5;
$a->v2 = 'Hello';
var_export($a);
eval('$b = ' . var_export($a, true) . ';');
echo "<br />";
var_export($b);
?></body>
</html>