An example. class classname { public $attribute; function operation($param) { $this->attribute = $param echo $this->attribute; } } I am having trouble understanding the $this pointer in the book I am reading. I cannot find it in the documentation on php.net. Why is this? How can I learn this? Thanks.
Operation is a custom function that takes one parameter - $param. Inside that function $this->attribute equals to $param and later $this->attribute (which equals to $param) is echoed out.
Thanks for your reply. Let me try to clear up any confusion. The example above was just to illustrate the $this pointer. There are more complicated examples in the book I am reading that I am having trouble understanding. My question is- where can I find documentation of the $this pointer that will explain its purpose and elaborate on its use? Thanks.
Check out http://www.phpfreaks.com/forums/index.php/topic,95867.0.html and http://www.php.net/manual/en/language.operators.php.
In short when you declare a class you don't know exactly the name of object of this class in future. For example for your code I can write: $first = new classname(); PHP: or $anyname = new classname(); PHP: So to refer to certain object of declared class in future $this is used. It is always an object of this class. So for my examples: Inside $first $this will refer to $first and inside $anyname $this refers to $anyname;
Thanks. I thought ($this) was a keyword and would have whole a page of discussion devoted to it. I did not find it in the list of keywords on php.net. I looked at your links. I did not find as of yet ($this) specifically discussed as a topic by itself, but I did learn that (->) is called an object operator. I have been wondering what that was called for some time.
Thanks AsHinE. Maybe the concept ($this) is so simple I am having trouble rapping my brain around it.