Entry
How can I call a polymorphed parent function?
I have a subclass with method foo(), can I call foo() in the parent?
May 30th, 2000 15:25
Bruce Fletcher, Nathan Wallace, Andi Gutmans, Nathan Wallace
For PHP4, use the syntax ParentClass::Function().
This is supported by the Zend engine and will thus be supported in PHP
4.0.
It's not possible in PHP 3.0.
There is a (hacky) work around however. The basic idea is to define a
unique method name in each class for the same method. Then the extended
class can reference directly to the unique method name in its parent.
To achieve the appearance of polymorphism when using the class you just
create a method with the desired name in every class definition that
calls the unique method name in that class. An example will explain it
better:
class A {
function A() { }
function A_dspTwo() {
echo "A: Two<br>";
}
function dspTwo() {
return $this->A_dspTwo(); // call the class A dspTwo method
}
}
class B extends A {
function B() {
$this->A(); // call the parent constructor.
}
function B_dspTwo() {
$this->A_dspTwo();
echo "B: Two<br>";
}
function dspTwo() {
return $this->B_dspTwo();
}
}
$object = new B();
$object->dspTwo();