I have a classB which extends classA. In classB I define a method fooBar() which is also defined in classA. In fooBar() of classB I want to call fooBar() of classA at the beginning. Just the way I'm used to, from Objective-C. Is that possible in PHP? And if so, how?
+1
A:
Just a quick note because this doesn't come up as easy on Google searches, and this is well documented in php docs if you can find it. If you have a subclass that needs to call the super class´s constructor, you can call it with:
parent::__construct(); // since PHP5
An example would be if the super class has some arguments in it's constructor and it's implementing classes needs to call that:
class Foo {
public function __construct($lol, $cat) {
// Do stuff specific for Foo
}
}
class Bar extends Foo {
public function __construct()(
parent::__construct("lol", "cat");
// Do stuff specific for Bar
}
}
You can find a more motivating example here.
Spoike
2010-09-09 05:20:51