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
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.
$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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With