CodeIgniter Forums
What does $this refer to in a Model? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: What does $this refer to in a Model? (/showthread.php?tid=64217)

Pages: 1 2


RE: What does $this refer to in a Model? - dangyuluo - 01-30-2016

(01-29-2016, 03:41 AM)Narf Wrote:
(01-29-2016, 03:06 AM)dangyuluo Wrote:
(01-29-2016, 02:06 AM)Narf Wrote: By actually reading my second sentence and those links? Smile

I'm not trying to offend you, but maybe you misunderstand my question.
I can surely use _get to get access to function and variable of CI_Controller. I have known that quite before.
But my question actually is, since I have experimented before, why can I use $this->cache->redis in a model, in which $this should refer to the Model Class, not the CI_Controller?

No offense taken, but you're contradicting yourself ...

If you knew how __get() works (that's two underscores, not one), then you wouldn't be asking this question. And if you don't understand it - you didn't really follow through that first link, which explains it:

Code:
$this->cache

Is interpreted by PHP (given the current state of your class) as:

Code:
[current class instance] -> [inaccessible or non-existent property]

And gets executed as:

Code:
[current class instance] -> __get([name of the inaccessible or non-existent property])

That's what __get() is made for, you don't use it as $this->__get($something) ... that'd be silly.


Ok guy, now I understand why you mentioned __get() function. Before that I thought it was only a function defined by Codeigniter. Thank you!