SeanColeman Posted February 1, 2007 Share Posted February 1, 2007 This may have been answered already, but try searching for $this. Anyhoo, I would love to find out definitively, without any doubt, what the heck it means? How is it used? (If you leave a code example, please use anything but FOO!! ) And, what is $this->output.= '</table>'; doiing? (Just an example) I know that it is somehow performing "echo" or "print" but what is -> doing and where is all this (pardon the pun) coming from? I'm a self taught PHP muddler, so the whole function/method thing is a vague concept to me. I get the jist, but not anywhere near a commanding understanding. I'm looking for the dumb-down explanation. Quote Link to comment https://forums.phpfreaks.com/topic/36684-end-my-this-pain/ Share on other sites More sharing options...
Balmung-San Posted February 1, 2007 Share Posted February 1, 2007 This (no pun intended) is based on Object Oriented standards. When you've written a class $this will literally mean this instance of this object. Example: <?php class A{ public $var1 = ""; public function speak(){ echo $this->var1; } } $a = new A(); $a->var1 = "hello\n"; $a->speak(); $b = new A(); $b->var1 = "world\n"; $b->speak(); $a->speak(); ?> This will output: hello world hello Quote Link to comment https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174906 Share on other sites More sharing options...
wildteen88 Posted February 1, 2007 Share Posted February 1, 2007 the variable $this is used to access objects (variables) and methods (functions) within a class. Quote Link to comment https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174907 Share on other sites More sharing options...
SeanColeman Posted February 1, 2007 Author Share Posted February 1, 2007 Wow. That means I'm about to get a bigger headache. I am playing with a 200 line page and it's all for a class with about way-too-many $thises (yep, that's a new word) in it. So each time I see $this, it is refering to any variable/functions within the class it's under? I feel my eyes bugging as I write. :'( Quote Link to comment https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174951 Share on other sites More sharing options...
Balmung-San Posted February 1, 2007 Share Posted February 1, 2007 Yup. That's exactly how it works. Just remember it's that instance of that object. Unless the variable is a constant (and static? do PHP statics work like Java statics?), it will probably be different for each instance of the class. Quote Link to comment https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174955 Share on other sites More sharing options...
SeanColeman Posted February 1, 2007 Author Share Posted February 1, 2007 Alrighty, so here's bit of the first few lines. It's a calendar thingy. First line of the included Class is protected $ID; So, that means $this->ID = $ID."_"; in the below is refering to $ID from the included file controls.php (Plus the underscore)? include_once("controls.php"); class Calendar extends Controls { ......... public function Calendar($ID, $Date = NULL) { $this->ID = $ID."_"; ... ... Quote Link to comment https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174961 Share on other sites More sharing options...
KrisNz Posted February 1, 2007 Share Posted February 1, 2007 Yup, whats its doing is setting the 'protected $ID' - a property of the calendar to the value contained in $ID - the first argument of the function. That particular function is what's known as a constructor, and its called automatically when you create an instance of the object. E.g $cal = new Calendar(1,'2007-01-11'); //I dont know how it expects the date to be formatted so that may not work, its just an example echo $cal->ID; //echos '1_' because the constructor has been called and the objects property has been set to 1 and concatenated with an underscore by the constructor Because that property is marked as protected, it means that its value can only be changed by that class or any class that extends it. So if you tried to go $cal->ID = "some crazy value"; You would get an error. Quote Link to comment https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-174979 Share on other sites More sharing options...
SeanColeman Posted February 1, 2007 Author Share Posted February 1, 2007 Thank you very much! My confusion was coming from the "extension" of the included file, had no idea that $this reached that document. This should definitely make it easier to pick it apart, but I'm sure I'll be back. I have to now figure out where the thing is hiding the resulting $_POST. Onward! And thanks again everyone. Quote Link to comment https://forums.phpfreaks.com/topic/36684-end-my-this-pain/#findComment-175063 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.