MacMac
MacMac

Reputation: 35301

What is the meaning of $this

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?

Upvotes: 2

Views: 9485

Answers (4)

Pekka
Pekka

Reputation: 449415

$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

Upvotes: 19

Glycerine
Glycerine

Reputation: 7347

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

Upvotes: 3

Neo
Neo

Reputation: 13891

$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.

Upvotes: 0

Tomasz Kowalczyk
Tomasz Kowalczyk

Reputation: 10467

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.

Upvotes: 4

Related Questions