Answer the question
In order to leave comments, you need to log in
How to access the constructor of the (first) parent class?
Hello.
During the discussion (well, you know, at such moments all sorts of thoughts come to mind, which is very good), the question arose - is it possible to call the constructor of the "first" parent class with such an inheritance hierarchy:
class Prnt {
public function __construct() {
echo 'In Prnt';
}
}
class A extends Prnt {
public function __construct() {
echo 'In A';
}
}
class B extends A {
public function __construct() {
echo 'In B';
}
}
Answer the question
In order to leave comments, you need to log in
class B extends A
{
public function __construct()
{
echo 'In B';
Prnt::__construct();
}
}
class B extends A
{
public function __construct()
{
echo 'In B';
$reflection = new ReflectionClass(self::class);
$parent = $reflection->getParentClass();
while ($parent->getParentClass()) {
$parent = $parent->getParentClass();
}
$class = $parent->getName();
$class::__construct();
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question