I have a drawing PHP class called ClassA that is extended by many other drawing classes, ClassB for instance.
I need the inherited classes to fire its parent classes’ Draw() method. However, in my particular situation, I do not want to call such method directly (e.g.: parent::Draw()). I’d like a third function (e.g.: parent::InvokeDraw()) to call my drawing method from within the parent’s context.
Here’s some code to illustrate:
class ClassA
{
function Draw()
{
/* Drawing code ... */
}
function InvokeDraw()
{
$this->Draw();
}
}
class ClassB extends ClassA
{
function Draw()
{
parent::InvokeDraw();
/* Drawing code ... */
}
}
The problem I’m facing is that InvokeDraw() will not call the parent’s Draw() method, but rather the extended class’ own Draw() method, thus causing an infinite loop.
Although the issue is fairly logical, I am having a hard time figuring out a workaround on that. How to accomplish this task?
Desired effect

Infinite loop problem

This one is with using static methods
Note that the only thing I changed is the
InvokeDraw()method and made it useselfwhich refers to a class, rather than object as it is with$thisOutput:
Edit:
To answer your comment below I will add a short description of how your code works and how this code works.
What happens in your code:
Band start working with itB->Draw()while working withinBclass ANDBobject.B->Draw()calls statically(that means class method)A::Invoke()from class A BUT we are still usingBobject.A::Invoke()calls$this->Draw();and as we are working currently withBobject,$thisrefers to an instance ofClassB.What happens in the code above:
Band start working with itB->Draw()while working withinBclass ANDBobject.B->Draw()calls statically(that means class method)A::Invokefrom class A BUT as well as in your code we are still usingBobject.A::Invoke()callsself::Draw()which is basically the same asClassA::Draw()and because it’s a static method, we don’t care what object we are currently working with and we call theA‘sDraw()method.A::Draw()method executes as we need.I will provide the same explanation for the code in my second answer, which doesn’t use static calls:
Band start working with itB->Draw()while working withinBclass ANDBobject.B->Draw()CREATES an instance ofA.B->Draw()callsA->Invoke()which means we start to work with an object that is instance of classAand notBlike before.At this point we completely forget that
Beven exists and work only withAA->Invoke()calls$this->Draw()which means that we are callingA->Draw(), because we already work with an instance of classA.A->Draw()executes as we expect.From usability point of view, we can see that the static method is better, as we can define some static properties and you can work with them when
A::Draw()executes. If we use non-static method, then we need to pass the data we need within arguments of our methods.I hope this makes it clear. The sequences above do not have the right terminology, but it was written on purpose, I think it’s easier to understand the flow that way.