Lets see a basic class:
class A
{
public function renderSomethingRecursive()
{
//this function can call itself
self::renderSomethingRecursive(); // ERROR!!!!
}
abstract public function addSomething (value);
}
class B extends A
{
public function renderSomethingRecursive()
{
throw new Exception ('This time this method must not be called!');
}
public function addSomething (value)
{
//something, something, something....
parent::renderSomethingRecursive();
}
}
$obj = new B();
$obj->addSomething(....);
when I do this, B::renderSomethingRecursive() still gets called, which I dont want… on A:renderSomethingRecursive() method, at self::renderSomethingRecursive(); // ERROR!!!! line calls B::renderSomethingRecursive(); instead of A::renderSomethingRecursive();, which is reasonable since $this is now B and not A… but then I have no idea how to dodge it.
Btw, I know B::renderSomethingRecursive() method should just be removed, but I wanted to keep it as a notice that it mustnt be called by accident. I wanted to make it “private” to make this unavailable, but as we know, its not possible 🙂
Any ideas?
Why have you defined the method in
Bif you don’t want it to be called. Just don’t define it, and there’s only 1 method PHP can call. That way, you don’t have to useparent::theMethod, you can easily just call it usingthis->theMethod();that’s all, really