2

Please explain me that for what $this and -> stands for...lets take the example of following code...

$this->convertNamesToCaptions($order, $formId)
Aaron W.
  • 9,254
  • 2
  • 34
  • 45
  • possible duplicate of [What does the variable $this mean in PHP?](http://stackoverflow.com/questions/1523479/what-does-the-variable-this-mean-in-php) – mario Apr 02 '12 at 10:42

6 Answers6

15

$this refers to the current object

Manual says:

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

Little example:

class Test
{
    private $var;

    public function func()
    {
        $this->var = 1;
        return $this->var;
    }
}

$obj = new Test();

$obj->func();
Bono
  • 4,757
  • 6
  • 48
  • 77
5

$this is reference to current object while inside that objects code.

You'll find more information in PHP OOP basics.

vartec
  • 131,205
  • 36
  • 218
  • 244
5

So, simply :

  • $this refers to current object instance
  • -> indicates that the part on the right is a method of an object

In other words :

$this->doSth() means : run method doSth of the same object.

Dr.Kameleon
  • 22,532
  • 20
  • 115
  • 223
2

$this hold the reference of the selected object in use, -> is an operator used to assign a method or property to an object reference.

Chibuzo
  • 6,112
  • 3
  • 29
  • 51
2

I think this page say's it all: http://php.net/manual/en/language.oop5.basic.php

"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)."

in few words it's the calling object.

raPHPid
  • 567
  • 2
  • 10
0

$this is a pointer which points to the current object and -> is an operator used to assign value to an object on right hand side.

sunmm
  • 1