This is a simple example of how to implement serialization using the __sleep and __wakeup magic methods...
<?php
class Student{
private $full_name = '';
private $score = 0;
private $grades = array();
public function __construct($full_name, $score, $grades)
{
$this->full_name = $full_name;
$this->grades = $grades;
$this->score = $score;
}
public function show()
{
echo $this->full_name;
}
function __sleep()
{
echo 'Going to sleep...';
return array('full_name', 'grades', 'score');
}
function __wakeup()
{
echo 'Waking up...';
}
}
?>
<?php
include 'student.class.php';
$student = new Student('bla bla', 'a', array('a' => 90, 'b' => 100));
$student->show();
echo "<br />\n";
$s = serialize($student);
echo $s ."<br />\n";
$fp = fopen('./uploads/session.s', 'w');
fwrite($fp, $s);
fclose($fp);
?>
<?php
include 'student.class.php';
$s = implode('', file("./uploads/session.s"));
echo $s ."<br />\n";
$a = unserialize($s);
$a->show();
?>