note 102117 added to oop4.magic-functions

From: Date: Thu, 27 Jan 2011 20:27:17 +0000
Subject: note 102117 added to oop4.magic-functions
Groups: php.notes 
Request: Send a blank email to php-notes+get-176399@lists.php.net to get a copy of this message
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(); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 85.64.14.226 ---- Manual Page -- http://www.php.net/manual/en/oop4.magic-functions.php Edit -- https://master.php.net/note/edit/102117 Del: integrated -- https://master.php.net/note/delete/102117/integrated Del: useless -- https://master.php.net/note/delete/102117/useless Del: bad code -- https://master.php.net/note/delete/102117/bad+code Del: spam -- https://master.php.net/note/delete/102117/spam Del: non-english -- https://master.php.net/note/delete/102117/non-english Del: in docs -- https://master.php.net/note/delete/102117/in+docs Del: other reasons-- https://master.php.net/note/delete/102117 Reject -- https://master.php.net/note/reject/102117 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#176399) next »