tags:

views:

148

answers:

4

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?

+13  A: 

$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
 {
   var $message = "The big brown fox... jumped....";

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

$my_object = new Classname();  // this is a valid object
$my_object->name = "Hello World!";
$my_object->showMessage();  // 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
Pekka
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
YouBook
I'm pretty sure showMessage will display "The big brown fox... jumped..." instead of "Hello World!". You probably meant $my_object->message
luiscubal
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
+3  A: 

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
+1  A: 

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->make('Ford');
$car->make('Escort');

echo $car->whatCar();
//This car is a Ford Escort
Glycerine
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
I plus'd one to you for my mistake. I'll leave the error in for reference but you are correct.
Glycerine
A: 

Hi,

$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