session的问题,书中的源码得不到正常结果
<?php
// Make sure to include class definition BEFORE session is started
class sleepy
{
var $status;
var $counter;
// Constructor
function sleepy()
{
$this->counter = 1;
$this->status = "I'm alive";
}
// Wakeup is called each time this object is deserialized
function __wakeup()
{
$this->counter++;
$this->status = "I'm awake";
}
// Sleep is called each time this object is serialized
function __sleep()
{
$this->status = "I'm asleep";
// If this array is not returned, the object is not serialized
return array("status", "counter");
}
} // end class sleepy
// Initiate the session
session_start();
if (!isset($_SESSION["sleepy_obj"]))
{
$sleepy_obj = new sleepy;
session_register("sleepy_obj");
}
?>
<html>
<head>
<title>Session and Objects</title>
</head>
<body>
<?php
$buf = sprintf("Status: %s. Counter: %s.", $_SESSION["sleepy_obj"]->status, $_SESSION["sleepy_obj"]->counter);
echo $buf;
?>
</body>
</html>