what is “$this” in php? [duplicate]

♀尐吖头ヾ 提交于 2019-12-06 09:38:32

问题


Possible Duplicate:
What does the variable $this mean in PHP?

I know this is extremely basic. I am watching tutorials on YouTube about CakePHP and PHP and all of them keep using "$this", but none of them actually say what it is or what it is used for. I think it is a variable, but what does it contain, and why do all of the functions have to be run from it? Is it the current class? I read here that "$this" is different from self though, so I am not sure if this is the case.


回答1:


if used in a class, $this refers to the object that it's in.




回答2:


$this refers to the instance of the class (a.k.a. object). self is more or less the same, but for static classes.




回答3:


I suggest you to read http://php.net/oop in particular this section http://www.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).



来源:https://stackoverflow.com/questions/6486943/what-is-this-in-php

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