0

我想使用 getter/setter 在我的模型中存储和检索值。下面是我的控制器和模型代码。我想知道这是一种标准的正确方法吗?

控制器代码:

  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();
}
}
4

1 回答 1

2

使用 getter 和 setter 是一种很好的做法,但没有必要这样做。但我建议您继续使用 setter 和 getter。

参考这些链接

为什么要使用 getter 和 setter? Getter 和 Setter?

于 2013-03-20T06:19:56.350 回答