Say I have class child()
and class parent()
. The parent has a constructor and a few other public methods, and the child is empty apart from a constructor.
How do I go about calling a parent's methods inside of the child's constructor, as in:
Class Parent {
public function __construct() {
// Do stuff (set up a db connection, for example)
}
public function run($someArgument) {
// Manipulation
return $modifiedArgument;
}
}
Class Child extends Parent {
public function __construct() {
// Access parent methods here?
}
}
Say I want to call parent
s run()
method, do I have to call a new instance of the parent inside the child constructor? Like so...
$var = new Parent();
$var->run($someArgument);
If so, what is the point of extends
from a class definition POV? I can call a new instance of another class with the new
keyword whether it extends the 'child' or not.
My (likely) wrong understanding was that by using extends
you can link classes and methods from a parent can be inherited into the child. Is that only outside the class definition? Does using extend
offer no efficiencies inside the class definition?
Because referring to the parent's run()
method with the this
keyword certainly doesn't work...