我有一个代码:
class db {
var $connection;
function escape($esc) {
return str_replace(array('%','_'),array('\%','\_'),mysqli_real_escape_string($this->connection,$esc));
}
[...]
}
$db=new db;
class Session {
private function read($sid) {
global $db;
$r=$db->query('SELECT `data` FROM `sess` WHERE `hash`=\''.$db->escape($sid).'\' LIMIT 1');
if ($this->debug) echo 'Read: <u>SELECT `data` FROM `sess` WHERE `hash`=\''.$db->escape($sid).'\' LIMIT 1</u><br/>';
if($db->num_rows($r)==1) {
$fields=$db->fetch_assoc($r);
return $fields['data'];
}
else return '';
}
private function write($sid, $data) {
global $db;
if ($this->debug) echo 'Write: <u>REPLACE INTO `sess`(`hash`,`data`) VALUES(\''.$db->escape($sid).'\',\''.$db->escape($data).'\')</u><br/>';
$db->query('REPLACE INTO `sess`(`hash`,`data`) VALUES(\''.$db->escape($sid).'\',\''.$db->escape($data).'\')');
return $db->connection->affected_rows;
}
[...]
function __construct($debug=false) {
session_set_save_handler(
array(&$this, 'open'),
array(&$this, 'close'),
array(&$this, 'read'),
array(&$this, 'write'),
array(&$this, 'destroy'),
array(&$this, 'clean')
);
$this->debug=$debug;
session_start();
}
}
$sessions=new Session(true);
我不断得到Fatal error: Call to a member function escape() on a non-object on line 61
(第 2 行function write($sid, $data)
)。奇怪的是,调试器显示该函数read
已成功执行。谁能解释一下为什么会发生这种情况?