I want to know how can i call constructor of super parent class from Nth (2nd or 3rd or Any) sub class.
class a{
public function __construct(){
echo "<br>In constructor of class A <br>";
}
}
class b extends a {
public function __construct(){
echo "<br>In constructor of class B <br>";
}
}
class c extends b {
public function __construct(){
parent::__construct();
echo "<br>In constructor of class C <br>";
}
}
$obj = new c;
What changes i need to make in constructor of class C above where parent's constructor is being called. Parent:: __construct(); referes to consructor of Class B where as i need to call constructor of class A from class C.
There is no way for you to call class a
's constructor from class c
using the parent
reference since there is no functionality provided to things such as parent::parent::etc
.
On another note: what you are doing kind of defeats the purpose of inheritance.. class c shouldn't be aware of the fact that it's base actually inherits class a.
If class b wanted class a's constructor to be run it should have implemented this, which yields the fact that if class c wants to call class a's constructor it should inherit from class a, not class b.
Hey, I wanna live on the edge - stop telling me what I cannot do!!one
Well, if you really want to hack your way around this you can explicitly call class a's constructor using the below:
class c extends b {
public function __construct(){
a::__construct ();
}
}