<?php
class a {
public function foo() {
echo __METHOD__ . PHP_EOL;
}
}
class b extends a {
public function foo() {
$this->foo(); # I want to call a's foo method here.
echo __METHOD__ . PHP_EOL;
}
}
class c extends a {
public function foo() {
echo __METHOD__ . PHP_EOL;
$this->foo(); # I want to call a's foo method here.
}
}
class d extends a {
# This class does not handle anything super special in our foo step, so I don't over write a's foo();
}
$a = new a; $a->foo();
$b = new b; $b->foo();
$c = new c; $c->foo();
$d = new d; $d->foo();
?>
I'm sure there are a few of you scratching your head trying to figure out why I would ever want to do something like this, so I promise you this will make sense, just let me explain.
I have a function that works just fine by it's self and I have some other classes that extend upon the base class to add some functions to it, these classes do the same task but in a different context or with additional information so they must do all of the steps of the parent class but they must also do some special sub set's in some cases.
Is there a way I can still execute a's foo method and my sub classes foo method?
I know I could fix this with some awful hacks like making the method a file, and then including that file in both the parent function and it's siblings where needed, or I could make a second method called bar() that would contain the instructions of foo() and have my sub classes overwrite foo() but call bar() when they need the functionality of foo(). I'm just wondering if I can do this without using some pretty interesting hacks, is there something I don't know about PHP that someone on here can teach me?