1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP $this-> What does it mean?

Discussion in 'PHP' started by tdd1984, Jun 4, 2008.

  1. #1
    Ok I'm confused now why it $this-> always used inside methods (inside classes)? I mean what does it do? I know it makes a reference, but I'm still kind of confused?
    SEMrush
     
    tdd1984, Jun 4, 2008 IP
    SEMrush
  2. itnashvilleCOM

    itnashvilleCOM Banned

    Messages:
    176
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #2
    It is a variable that can be passed from function to function within a class.
     
    itnashvilleCOM, Jun 4, 2008 IP
  3. xlcho

    xlcho Guest

    Messages:
    532
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $this is a pointer to the current object of the class. Meaning if you are working with the class $foo, that has a member variable $var, when working with objects from that class, you can use $this->var to access the current object's $var.
     
    xlcho, Jun 5, 2008 IP
  4. aboyd

    aboyd Well-Known Member

    Messages:
    158
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    138
    #4
    So the idea of an object is that everything inside of it is part of the object rather than just splattered into the global namespace. In other words, if you have 2 methods inside your object -- say, getStuff() and setStuff() -- then you can't just call 'em from anywhere. They aren't visible everywhere. If you have a third function and you try to call getStuff() raw, just like that, it won't work. Why? Because getStuff() is part of the object. It isn't visible to other code. So you have to call it as part of the object, like this: $objectName->getStuff().

    Well, when you're INSIDE the object, you wouldn't create an object to talk to itself. It's already created. So when one part of an object wants to use another part of the same object, it uses $this, as in, "the object I'm inside of, dude." So this is how setStuff() might call getStuff(): $this->getStuff(). It means, "run the method that's in the same class that I'M in."
     
    aboyd, Jun 5, 2008 IP
  5. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #5
    Danltn, Jun 7, 2008 IP