我想使用getter / setter在我的Model中存储和检索值.Below是我的Controller和Model代码。我想知道它的标准正确方法吗?
控制器代码:
public function saveevent() {
$this->event_model->setEvent_title($this->input->post('event_title'));
$this->event_model->setSms_description($this->input->post('sms_description'));
$event_id = $this->event_model->saveEvent();
}
标准代码:
class Event_Model extends CI_Model{
private $id;
private $event_title;
private $sms_description;
function __construct() {
parent::__construct();
}
public function getId() {
return $this->id;
}
public function setId($id) {
$this->id = $id;
}
public function getEvent_title() {
return $this->event_title;
}
public function setEvent_title($event_title) {
$this->event_title = $event_title;
}
public function getSms_description() {
return $this->sms_description;
}
public function setSms_description($sms_description) {
$this->sms_description = $sms_description;
}
public function saveEvent() {
$data = array(
'title' => $this->getEvent_title() ,
'sms_description' => $this->getSms_description()
);
$this->db->insert('events', $data);
return $this->db->insert_id();
}
}
答案 0 :(得分:2)
使用getter和setter是一种很好的做法,但没有必要这样做。但我会建议你继续使用setter和getters。
参考这些链接