Php Codeigniter-使用getter/setter将值从控制器传递到模型
我想使用getter/setter在我的模型中存储和检索值。我想知道这是一个标准的正确方法吗 控制器代码:Php Codeigniter-使用getter/setter将值从控制器传递到模型,php,oop,codeigniter,Php,Oop,Codeigniter,我想使用getter/setter在我的模型中存储和检索值。我想知道这是一个标准的正确方法吗 控制器代码: public function saveevent() { $this->event_model->setEvent_title($this->input->post('event_title')); $this->event_model->setSms_description($this->input->post('sms_d
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();
}
}
使用getter和setter是一个很好的实践,但是没有必要这样做。但我建议您继续使用setter和getter 请参考这些链接
谢谢你提供的信息。这很有用。他们认为我实施的方式是正确的?@MDeSilva请不要要求人们对你的答案进行评分/投票/接受。欢迎他们在没有任何压力的情况下选择如何行动。