php - What is the usage of property in Codeigniter Model Class? -
i copied following code official codeigniter documentation.
class blog_model extends ci_model { public $title; public $content; public $date; public function get_last_ten_entries() { $query = $this->db->get('entries', 10); return $query->result(); } public function insert_entry() { $this->title = $_post['title']; // please read below note $this->content = $_post['content']; $this->date = time(); $this->db->insert('entries', $this); } public function update_entry() { $this->title = $_post['title']; $this->content = $_post['content']; $this->date = time(); $this->db->update('entries', $this, array('id' => $_post['id'])); } } please check line 3-5 : there 3 property called public $title; public $content;public $date;.
what usage of them?
i asked question because codes works fine when removing them.
i have tested removing there properties. still can call get_last_ten_entries(), insert_entry(), update_entry() function controller without issue.
these public variables
a public scope can make variable/function available anywhere, other classes , instances of object. (meaning once value assigned them, can access other class or instace)
so why can still work functions?
get_last_ten_entries(): functions not require variablesinsert_entry()&update_entry(): assigning value them.
you'll error if request value instead of assigning value.
for example (assuming deleted public variables) , call function like
public function request_title(){ return $this->$title; } without assigning values previously.
hope helps
Comments
Post a Comment