$this
is a reference to the current object.
It can be used in class methods only.
From the manual:
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).
a simple real world example:
class Classname
{
private $message = "The big brown fox... jumped....";
function setMessage($givenMessage) {
$this->message = $givenMessage;
}
function getMessage() {
return $this->message; // Will output whatever value
// the object's message variable was set to
}
}
$my_object = new Classname(); // this is a valid object
echo $my_object->getMessage(); // Will output "The big brown fox... jumped...."
$my_object->setMessage("Hello World!");
echo $my_object->getMessage(); // Will output "Hello world"
$this
is not available when you call a method in a static context:
Classname::showMessage(); // Will throw an error:
// `$this` used while not in object context