Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does $this actually mean> Codeigniter

Silly question I know,

From all the tutorials they do not explain why they use $this.

Is $this like an object from a base class in Codeigniter?

Any explanation would be welcomed! :)

Thanks

like image 266
sqlmole Avatar asked Aug 24 '11 19:08

sqlmole


2 Answers

To actually answer your question, $this actually represents the singleton Codeigniter instance (which is actually the controller object).

For example when you load libraries/models, you're attaching them to this instance so you can reference them as a property of this instance.

Another way to get this instance in codeigniter is the get_instance() function that you use when building portable libraries.

like image 140
jondavidjohn Avatar answered Sep 19 '22 01:09

jondavidjohn


$this in PHP is the current object. In a class definition, you use $this to work with the current object. Take this class as an example:

class Hello {
  public $data = 'hello';
  function hi() {
    $this->data = 'hi';
  }
}

You can instantiate this class multiple times, but $data will only be changed to hi in those objects where you called the function:

$one = new Hello;
$two = new Hello;
$two->hi();

echo $one->data, "\n", $two->data;
like image 40
Emil Vikström Avatar answered Sep 20 '22 01:09

Emil Vikström