What is $this keyword meant for?

血红的双手。 提交于 2019-12-08 08:59:37

问题


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

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

回答1:


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



回答2:


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

You'll find more information in PHP OOP basics.




回答3:


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.




回答4:


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




回答5:


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.




回答6:


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



来源:https://stackoverflow.com/questions/9974804/what-is-this-keyword-meant-for

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!