mirror of
https://github.com/php/presentations.git
synced 2026-03-24 07:32:11 +01:00
40 lines
740 B
PHP
40 lines
740 B
PHP
<?php
|
|
define('SERIALIZED', './presentations/slides/php5intro/serialized');
|
|
|
|
interface ISerializable {
|
|
function sleep();
|
|
function wakeup();
|
|
};
|
|
|
|
class Person implements ISerializable {
|
|
public $name;
|
|
|
|
function sleep() {
|
|
$f = fopen(SERIALIZED,'w');
|
|
fwrite($f,serialize($this->name));
|
|
fclose($f);
|
|
}
|
|
|
|
function wakeup() {
|
|
$this->name = @unserialize(
|
|
file_get_contents(SERIALIZED)
|
|
);
|
|
}
|
|
}
|
|
|
|
$p = new Person;
|
|
if ($p instanceof ISerializable) {
|
|
$p->wakeup();
|
|
}
|
|
echo "Previous Spy: {$p->name}\n<br />\n";
|
|
$superspies = array('James Bond',
|
|
'Sterling Hughes',
|
|
'Austin Powers');
|
|
$p->name = $superspies[array_rand($superspies)];
|
|
echo "New Spy: {$p->name}\n<br />\n";
|
|
|
|
if ($p instanceof ISerializable) {
|
|
$p->sleep();
|
|
}
|
|
?>
|