12

Possible Duplicate:
PHP: self vs this

Hello, Could you help me understanding the meaning of the PHP variable name $this?

Thank you for your help.

Community
  • 1
  • 1
Xela C
  • 121
  • 1
  • 1
  • 3
  • 2
    Possible duplicate: http://stackoverflow.com/questions/151969/php-self-vs-this. Also, please don't be too cute in questions. ;) – wkl Nov 08 '10 at 15:00
  • 2
    Congratulations on expanding your knowledge at the age of 12! But since that's not relevant to the question, could you edit the question to remove it? – Michael Paulukonis Nov 08 '10 at 15:02
  • It appears that people on Stack Overflow do not know what 'I am 12 years old and what is this' refers to. – JAL Nov 08 '10 at 15:06
  • 2
    How is this a dupe, he is not comparing the usage of `self` versus `$this` is specifically wanted to know **what** `$this` means. – RobertPitt Nov 08 '10 at 15:13
  • http://www.urbandictionary.com/define.php?term=I'm%2012%20years%20old%20and%20what%20is%20this%3F – flayn Nov 08 '10 at 15:27

3 Answers3

17

$this refers to the class you are in.

For example

Class Car {

    function test() {
        return "Test function called";
    }

    function another_test() {
        echo $this->test(); // This will echo "Test function called";
    }
}

Hope this helps.

Rotimi
  • 4,783
  • 4
  • 18
  • 27
jodm
  • 2,607
  • 3
  • 25
  • 40
  • It is a way of referencing itself... or calling methods or reading variables from itself. – jodm Nov 08 '10 at 15:02
  • 5
    actually, self refers to the current class you are in. $this refers to the current object instance of the class you are in. – Stephen Watkins Nov 08 '10 at 15:02
  • It won't `echo "Test functioned called"` since you're accessing the `test` member variable (which doesn't exist) as opposed to the method `test()`. You'd need to change it to `echo $this->test()`... – ircmaxell Nov 08 '10 at 15:03
  • It's not really the *class*, but the *object* - an *instance* of a class. – Pointy Nov 08 '10 at 15:03
  • 1
    Well actually its an instance of that class, where as `self` would refer to the class statically, and `__CLASS__` would refer to the classname as a string :-) – prodigitalson Nov 08 '10 at 15:03
  • @jodm, I edited the question and added the needed parentheses, hope you don't mind. – Adriano Varoli Piazza Nov 08 '10 at 15:07
2

You might want to have a look at the answers in In PHP5, what is the difference between using self and $this? When is each appropriate?

Basically, $this refers to the current object.

Community
  • 1
  • 1
Michael Paulukonis
  • 9,020
  • 5
  • 48
  • 68
1

$this is a protected variable that's used within a object, $this allows you to access a class file internally.

Example

Class Xela
{
   var age; //Point 1

   public function __construct($age)
   {
      $this->setAge($age); //setAge is called by $this internally so the private method will be run
   }

   private function setAge($age)
   {
      $this->age = $age; //$this->age is the variable set at point 1
   }
}

Its basically a variable scope issue, $this is only allowed within a object that has been initiated and refers to that object and its parents only, you can run private methods and set private variables where as out side the scope you cannot.

also the self keyword is very similar apart from it refers to static methods within class, static basically means that you cant use $this as its not an object yet, you must use self::setAge(); and if that setAge method is declared static then you cannot call it from an instant of that object / object

Some links for you to get started:

Community
  • 1
  • 1
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • While I understand you are explaining to an OOP newcomer, please do not refer to objects as classes. $this is used to refer to object properties and methods, while self:: is used to refer to class properties and methods. I think it is important to explain the difference between the two. – Craige Nov 08 '10 at 15:24