here is a code:
abstract class A
{
abstract public function add();
public function callItself()
{
echo __CLASS__.':'.__FUNCTION__.' ('.__LINE__.')<br>';
if (rand(1,100) == 5) { die; }
$this->callItself();
}
}
class B extends A
{
public function add()
{
echo __CLASS__.':'.__FUNCTION__.' ('.__LINE__.')<br>';
}
}
class C extends B
{
public function add()
{
echo __CLASS__.':'.__FUNCTION__.' ('.__LINE__.')<br>';
parent::add();
parent::callItself();
}
public function callItself()
{
echo __CLASS__.':'.__FUNCTION__.' ('.__LINE__.')<br>';
throw new Expection('You must not call this method');
}
}
$a = new C();
$a->add();
die;
in class C the callItself() must not be called, so it drops an exception. I cant set it private as we know 🙂 but at 10 line, the $this->callItself(); calls that method of **C** instead of A so it dies. But I dont want it, how to dodge that?
Use
self::callItself()instead of$this->callItself();Replace
With
Output