Where is documentation for ($this pointer)?

Discussion in 'PHP' started by Mitchell, Jan 9, 2010.

  1. #1
    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.
     
    Mitchell, Jan 9, 2010 IP
  2. iAreCow

    iAreCow Peon

    Messages:
    85
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    0
    #2
    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.
     
    iAreCow, Jan 9, 2010 IP
  3. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    Mitchell, Jan 9, 2010 IP
  4. CoreyPeerFly

    CoreyPeerFly Notable Member Affiliate Manager

    Messages:
    394
    Likes Received:
    24
    Best Answers:
    5
    Trophy Points:
    240
    #4
    CoreyPeerFly, Jan 9, 2010 IP
  5. AsHinE

    AsHinE Well-Known Member

    Messages:
    240
    Likes Received:
    8
    Best Answers:
    1
    Trophy Points:
    138
    #5
    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;
     
    AsHinE, Jan 9, 2010 IP
  6. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    Last edited: Jan 9, 2010
    Mitchell, Jan 9, 2010 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    JAY6390, Jan 9, 2010 IP
  8. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Thanks AsHinE.

    Maybe the concept ($this) is so simple I am having trouble rapping my brain around it.
     
    Mitchell, Jan 9, 2010 IP
  9. Mitchell

    Mitchell Peon

    Messages:
    204
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks JAY6390.

    I will study that page you sent.
     
    Mitchell, Jan 9, 2010 IP