-5

I copied following code from 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 the 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 are three property called public $title; public $content;public $date;.

What is the usage of them?

I asked this question because this codes works fine even when removing them.

I have tested it removing those there properties. Still I can call get_last_ten_entries(), insert_entry(), update_entry() function from Controller without issue.

  • Downvoters... Please can you tell me that why did you down vote my question? It will help me to ask better questions in future. – I am the Most Stupid Person Sep 18 '17 at 06:25
  • Hey, you can check this https://stackoverflow.com/questions/4361553/what-is-the-difference-between-public-private-and-protected for using public, private or protected. – Harish Sep 19 '17 at 11:02

3 Answers3

4

these are public variables

A public scope can make a variable/function available from anywhere, other classes and instances of the object. (meaning once a value is assigned to them, you can access the from any other class or instace)

So why can you still work with the functions?

  • get_last_ten_entries(): this functions does not require the variables
  • insert_entry() & update_entry(): You are assigning a value to them.

you'll get an error if you request their value instead of assigning the value.

for example (assuming you deleted the public variables) and then you call a function like

public function request_title(){
    return $this->$title;
}

without assigning values previously.

Hope this helps

Jefferson
  • 794
  • 10
  • 24
  • In other words, the call to `$this->title = $_POST['title'];` creates a public property ($title) for the class. Not defining the property in the class file works here but might not in other circumstances and could lead to hard to diagnose bugs. It also makes the class less "readable". This answer deserves to be marked as correct. – DFriend Sep 20 '17 at 14:13
0

These are the public variables. when You delete them and run your functions get_last_ten_entries(), insert_entry() and update_entry() ,Then it runs fine because values are assigned dynamically in the variable.

$this->title    = $_POST['title']; // please read the below note
                $this->content  = $_POST['content'];
                $this->date     = time();

here these variables created dynamically and it runs without error.

But, If you will use a function like *

public function getVal(){
     return $this->title;
}

*

It will throw an error,because there is not any earlier declaration of this variable .

0

Those three are properties as I think you've already guessed. But the reason why this still work even though they are removed from the class definition is that PHP allows dynamically declared properties. It means you can add properties any time you wish.

This is a PHP issue and not isolated to CodeIgniter

Check out: Can you create instance properties dynamically in PHP?

aljo f
  • 2,430
  • 20
  • 22