Developing Developer
Developing Developer

Reputation: 113

What is $this keyword meant for?

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

$this->convertNamesToCaptions($order, $formId)

Upvotes: 2

Views: 14391

Answers (6)

sunmm
sunmm

Reputation: 1

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

Upvotes: 0

Bono
Bono

Reputation: 4849

$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();

Upvotes: 15

Dr.Kameleon
Dr.Kameleon

Reputation: 22810

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.

Upvotes: 5

raPHPid
raPHPid

Reputation: 567

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.

Upvotes: 2

Chibuzo
Chibuzo

Reputation: 6117

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

Upvotes: 2

vartec
vartec

Reputation: 134581

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

You'll find more information in PHP OOP basics.

Upvotes: 5

Related Questions