When you have a class - existing object -Example:
class Object
{
protected $variable;
public function method($param) {}
}
$this
is just a way of telling ME
.
For example when you go and start eating you make it like that
ME::getSpoon();
ME::UseSPoonToFetchSoup();
ME::BringSpoonToMouth();
ME::PutSpoonInMouth();
ME::TakeSpoonOut();
ME::Swallow():
And then repeat some of the steps.
The ME
is reference for YOU yourself. If you think about that - this algorithm is true for almost everyone and each of existing ppl could use it - just remembering that ME
is the actual ,,human'' eating soup with the spoon.
Same is for object. In this example if you change the code a bit to
class Object
{
protected $variable = 0;
public function method($param = 1) {$this->variable += $param; return $this->variable;}
}
Executing method method();
without parameters would add into object OWN memory place for $variable
1 and then tells you new sum.
$obj1 = new Object();
$obj2 = new Object();
echo "Obj1: " .$obj->method(10).'<br/>'; //this will print 10
echo "Obj2: " .$obj->method(2).'<br/>'; //;this will print 2
This objects must know who they are
therefore needs to know the reference for themselves and this is where that $this
same for.
A wish this explanation is quite ease and understandable.