2

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.

Community
  • 1
  • 1
Josh Sherick
  • 2,161
  • 3
  • 20
  • 37

3 Answers3

8

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

SDuke
  • 437
  • 2
  • 4
  • 15
4

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

planestepper
  • 3,277
  • 26
  • 38
2

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

sahid
  • 2,570
  • 19
  • 25