数据对象映射模式概念
将对象和数据存储映射起来,对一个对象的操作会映射为对数据存储的操作
数据对象映射模式实例
db = Factory::getDatabase();
$res = $this->db->query("select * from user where id = $id limit 1");
$this->data = $res->fetch_assoc();
$this->id = $id;
}
function __get($key)
{
if (isset($this->data[$key]))
{
return $this->data[$key];
}
}
function __set($key, $value)
{
$this->data[$key] = $value;
$this->change = true;
}
function __destruct()
{
if ($this->change)
{
foreach ($this->data as $k => $v)
{
$fields[] = "$k = '{$v}'";
}
$this->db->query("update user set " . implode(', ', $fields) . "where
id = {$this->id} limit 1");
}
}
}
$user = new User(1);
$user->mobile = '18888888888';
$user->name = 'test';