2

I've seen some scripts contain $this in a PHP script that has OOP, I never knew the meaning of it... like

$this->refresh();

Perhaps explain to me what $this refers to be...?

But I do know that you cannot use it as a dynamic variable like $this_is_a_variable but why can't use it as a dynamic variable?

MacMac
  • 34,294
  • 55
  • 151
  • 222
  • http://stackoverflow.com/questions/2094052/how-to-explain-this-keyword-in-a-best-and-simple-way – Naveed Sep 12 '10 at 22:30

4 Answers4

19

$this is a reference to the current object.

It can be used in class methods only.

From the manual:

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

a simple real world example:

class Classname
{
  private $message = "The big brown fox... jumped....";

  function setMessage($givenMessage) {
    $this->message = $givenMessage;
  }

  function getMessage() {
    return $this->message;  // Will output whatever value 
                            // the object's message variable was set to
  }
}

$my_object = new Classname();  // this is a valid object
echo $my_object->getMessage();  // Will output "The big brown fox... jumped...."

$my_object->setMessage("Hello World!");
echo $my_object->getMessage();  // Will output "Hello world"

$this is not available when you call a method in a static context:

Classname::showMessage(); // Will throw an error: 
                          // `$this` used while not in object context
Martijn
  • 15,791
  • 4
  • 36
  • 68
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 1
    Well, that was a pretty fast answer Pekka :P I'll read the manual, I didn't know where to find about `$this` in the PHP Manual/Documentation. Thanks. :D – MacMac Sep 12 '10 at 22:18
  • 3
    I'm pretty sure showMessage will display "The big brown fox... jumped..." instead of "Hello World!". You probably meant $my_object->message – luiscubal Sep 12 '10 at 22:28
  • 2
    the use of 'var' should be replaced by 'private' or even 'protected' for class members. (see http://www.php.net/manual/en/language.oop5.properties.php) – Yanick Rochon Sep 12 '10 at 22:33
  • I've updated this answer to use a getter and a setter, which is more common for a basic class :) – Martijn Jun 18 '18 at 21:02
4

If you are doing OOP then you use classes. You can have:

class CFoo
  {
  private $var;
  public function setFoo($fooVal)
    {
    $this->var = $fooVal;
    }
  }

$this refers to the current object of that class.

Tomasz Kowalczyk
  • 10,472
  • 6
  • 52
  • 68
3

When creating classes within PHP, at times you may need to reference the class* itself. The $this variable is reserved for this purpose.

**This should be correct as 'referring to the object created' not the class. This is semantically more correct.*

For example:

class Car
{
    private $make;

    public function setMake($make)
    {
         $this->make = $make;
    }

    public function setModel($model)
    {
         $this->model = $model;
    }

    public function whatCar()
    {
        return "This car is a " . $this->make . " " . $this->model;
    }
}

And to use it would look something like:

$car = new Car();

$car->setMake('Ford');
$car->setModel('Escort');

echo $car->whatCar();
//This car is a Ford Escort
Community
  • 1
  • 1
Glycerine
  • 7,157
  • 4
  • 39
  • 65
  • 3
    I realize I'm nitpicking here, but `$this` references an *object* of the given class. To refer to the *class* itself, the `__CLASS__` magic constant is used. – BoltClock Sep 12 '10 at 22:28
  • I plus'd one to you for my mistake. I'll leave the error in for reference but you are correct. – Glycerine Sep 12 '10 at 22:44
0

$this refers to the current object of the class. $this is used in methods which are members of a particular class. Hence, inside those methods, the method already has the information about the particular "instance" of that class. So $this can be directly used to refer the current object, rather than retrieving and assigning an object to a different variable.

Neo
  • 13,179
  • 18
  • 55
  • 80