On a superficial level, inheritance works like merging code. Inherited methods are part of the child class as if they were native to the class:
class A {
public function foo() {
echo 'foo';
}
public function bar() {
echo 'bar';
}
}
class B extends A { }
For all intents and purposes, this is equivalent to writing:
class B {
public function foo() {
echo 'foo';
}
public function bar() {
echo 'bar';
}
}
Class B
has the function foo
and bar
as if they were part of its declaration.
Overriding methods in a child class replaces the one specific implementation with another:
class B extends A {
public function bar() {
echo 'baz';
}
}
This is as if B
was declared like this:
class B {
public function foo() {
echo 'foo';
}
public function bar() {
echo 'baz';
}
}
With one exception: the parent's implementation of a method is available using the parent
keyword. It does not change the context in which the code is executed, is merely uses the parent's implementation of the method:
class B extends A {
public function bar() { public function bar() {
parent::bar(); ---> echo 'bar';
} }
}
It works in the same context of the current instance of B
, it just pulls the old code of the parent out of the ether.
If you call another method in the parent's method, it executes in the context of the child, not in the context of the parent class. Because you have not instantiated the parent, you are working with a child. To demonstrate with properties (it works the same with methods):
class A {
protected $value = 'bar';
public function bar() {
echo $this->value;
}
}
class B extends A {
protected $value = 'baz';
public function bar() {
parent::bar(); // outputs "baz"
echo $this->value; // outputs "baz"
}
}
As such, there is no solution to your problem. You cannot call a method in the "parent's context". You can call code of the parent in the current context, that's all. What you're creating is a simple loop, because it doesn't matter whether the code is inherited or not, it all works in the same context.
You need to redesign that class to not call methods in a loop.