What does $this mean?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sequalit
Forum Commoner
Posts: 75
Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas

What does $this mean?

Post by Sequalit »

What does

Code: Select all

$this->
mean in PHP????
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it's used inside objects, it is a reference to the current instance of the object.. it's used to access other information in the object, normally instance sensitive.. such as variables set during operation of the object that vary between creations..
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post by sweatje »

$this is the current instance of a class. $this->var allows you to set or read attributes of the instance. $this->method() allows you to run methods of the class.
Sequalit
Forum Commoner
Posts: 75
Joined: Wed Oct 12, 2005 9:57 pm
Location: Texas

Post by Sequalit »

thanks for the fast response, i believe i understand it now =D :D
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

class foobar {
   function foobar() {
      $this->fooVar = 'fooVar!!!!';
   }

   function getFooVar() {
      return $this->fooVar;
   }
}

$foo &= foobar();
echo $foo->getFooVar();
just in case you didn't get it ;)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

This is a thinly related subject, but are self::method() and $this->method(); the same, with the exception of the SRO being a static call? Or would self::method() be the same as calling className::method() from outside of the class?
Post Reply