What does $this refer to in a Model? |
When initialising a model(such as a class called Mall), I can pass a TRUE in the third parameter to automatically connect to the default database. Then a member named mall is assigned to CI_Controller and I can use $this->mall->foo() to use the function defined in the Model.
And I notice that, in the Loader:database(), the 'db' variable is assigned to $CI, which is the CI_Controller super object. But here comes the question: In the model I can still use $this->db-> to get access to DB handler. Does that means that "$this" in a Model refers to CI_Controller? If so, where can I find the detailed definition? Thanks.
No, $this always refers to the instance of the class you are currently in.
You can access controller properties because of a magic __get() method: https://github.com/bcit-ci/CodeIgniter/b...el.php#L71 (01-28-2016, 10:31 AM)Narf Wrote: No, $this always refers to the instance of the class you are currently in. I know that $this refers to the most direct class, but how to explain that in a Model, $this can call the function or variable of CI_Controller? (01-29-2016, 02:06 AM)Narf Wrote: By actually reading my second sentence and those links? 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? (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? 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. (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? When my grand kids ask me: Grampa! What did you really accomplish in your life? I will answer: Well I had a huge impact on the CodeIgniter community by making one of the most knowledgeable person on that site become a lot more patient ![]() (01-29-2016, 11:32 AM)LeMec Wrote: When my grand kids ask me: Grampa! What did you really accomplish in your life? So typical of ancestors, to over-value themselves in front of the little kids. ![]() Seriously, don't flatter yourself. I didn't suddently become any more patient and at this rate, the only thing you're accomplishing is making foes. (01-29-2016, 12:45 PM)Narf Wrote:(01-29-2016, 11:32 AM)LeMec Wrote: When my grand kids ask me: Grampa! What did you really accomplish in your life? That's ok...I am making new friends now...somewhere else ![]() (01-29-2016, 11:32 AM)LeMec Wrote:(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? LeMec, I don't understand why you would post such a thing. Here is Narf trying to help somebody, and you are like a heckler, a court jester, or maybe just a modern forum troll. The last thread I watched you basically hijack was all about your personal grudge against Narf and his lack of explaining things to your satisfaction. In the end, the OP was satisfied, and he/she seemed confused or amused about your rant than anything else. It's pretty rude to hijack threads, and I don't have any moderation powers here, but if I did I'd be scolding you right now. |
Welcome Guest, Not a member yet? Register Sign In |