-1
這是我的班,這個班的實例知道他們應該什麼時候保存這個值。你對這個想法有什麼看法,以及這個實現有什麼優點和缺點?我應該將數據保存在類的析構函數中嗎?
class Model_User_Doc extends ArrayObject {
protected $_id;
protected $_value;
protected $_db;
protected $_changed = FALSE;
public function __construct($id = NULL) {
if ($id !== NULL) {
$this->_id = $id;
$this->_db = DB::instance();
$this->_value = $this->_db->get($id);
}
}
public function __set($key, $value) {
$this->_changed = TRUE;
$this->_value[$key] = $value;
}
public function __get($key) {
if (isset($this->_value[$key])) {
return $this->_value[$key];
}
return NULL;
}
public function __unset($key) {
$result = FALSE;
if (isset($this->_value[$key])) {
$this->_changed = TRUE;
unset($this->_value[$key]);
$result = TRUE;
}
return $result;
}
public function offsetGet($name) {
return $this->_value[$name];
}
public function offsetSet($name, $value) {
$this->_changed = TRUE;
$this->_value[$name] = $value;
}
public function offsetExists($name) {
return isset($this->_value[$name]);
}
public function offsetUnset($name) {
$this->_changed = TRUE;
unset($this->_value[$name]);
}
public function cas() {
if ($this->_changed === TRUE) {
$this->save();
}
}
public function save() {
$this->_db->set($this->_id, $this->_value);
$this->_changed = FALSE;
}
public function __destruct() {
$this->cas();
}
}
?>
這個問題可能是更適合_CODE Review_。 – halfer