In Very Simple English:
Once inside an object's function, you have complete access to its variables, but to set them you need to be more specific than just using the variable name you want to work with. To properly specify you want to work with a local variable, you need to use the special $this
variable, which PHP always sets to point to the object you are currently working with.
For example:
function bark()
{
print "{$this->Name} says Woof!\n";
}
Whenever you are inside a function of an object, PHP automatically sets the $this
variable contains that object. You do not need to do anything to have access to it.
In Normal English:
$this
is a pseudo-variable which is available when a method is called from within an object context. It 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)
An example:
<?php
class A
{
function foo()
{
if (isset($this)) {
echo '$this is defined (';
echo get_class($this);
echo ")\n";
} else {
echo "\$this is not defined.\n";
}
}
}
class B
{
function bar()
{
// Note: the next line will issue a warning if E_STRICT is enabled.
A::foo();
}
}
$a = new A();
$a->foo();
// Note: the next line will issue a warning if E_STRICT is enabled.
A::foo();
$b = new B();
$b->bar();
// Note: the next line will issue a warning if E_STRICT is enabled.
B::bar();
?>
Output:
$this is defined (A)
$this is not defined.
$this is defined (B)
$this is not defined.