what is "$this" in php? [duplicate]
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 o开发者_Python百科f 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.
if used in a class, $this
refers to the object that it's in.
$this refers to the instance of the class (a.k.a. object). self is more or less the same, but for static classes.
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).
精彩评论