PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
class Test
{
function echoTest()
{
echo 'I am Test.';
}
}
class Tester extends Test
{
function echoTester()
{
parent::echoTest();
}
}
$tester = &new Tester();
$tester->echoTester();
class Test
{
function echoTest()
{
echo 'I am Test.';
}
}
class Tester extends Test
{
function echoTester()
{
$this->echoTest();
}
}
$tester = &new Tester();
$tester->echoTester();
Why do they have 'parent::' then? When will I want to use it instead of '$this->'?
Yeah, but back to my question, what's the difference between parent::method() and $this->method()? When will I want to use one and not another? Or is there absolutely no difference at all?
using "parent" will directly call the parent of the class you are currently in, while using $this may call something at your current depth as opposed to your parent. "self" is correctly used to call a static function or variable of the current class, wherever it resides (lineage-wise) unless you aren't given access to it, via an access modifier such as "private"