note 102117 deleted from oop4.magic-functions by sobak

From: Date: Fri, 25 Nov 2016 08:17:32 +0000
Subject: note 102117 deleted from oop4.magic-functions by sobak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-207686@lists.php.net to get a copy of this message
Note Submitter: ranware2200 at yahoo dot com ---- This is a simple example of how to implement serialization using the __sleep and __wakeup magic methods... <?php //student.class.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 //a.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 //b.php include 'student.class.php'; $s = implode('', file("./uploads/session.s")); echo $s ."<br />\n"; $a = unserialize($s); $a->show(); ?>

« previous php.notes (#207686) next »