I know that inside a class, $this
is used to refer to the current object and self
is used to refer to the current class. For example:
class My_Class {
function start1() {
$var = self::hello();
echo $var;
}
function start2() {
$var = $this->hello();
echo $var;
}
function hello() {
return 'Hello';
}
}
$obj = new My_Class;
$obj->start1(); // Hello
$obj->start2(); // Hello
In my example, both $this->hello()
and self::hello()
appear to do the exact same thing. Can anyone give an example of when I should use $this
and when I should use self
?