0
0
PHPprogramming~20 mins

Constructor inheritance in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constructor Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of constructor inheritance with parent::__construct() call
What is the output of this PHP code when creating an instance of class Child?
PHP
<?php
class ParentClass {
    public function __construct() {
        echo "Parent constructor called. ";
    }
}

class Child extends ParentClass {
    public function __construct() {
        parent::__construct();
        echo "Child constructor called.";
    }
}

new Child();
?>
AChild constructor called.
BParent constructor called. Child constructor called.
CParent constructor called.
DFatal error: Call to undefined method parent::__construct()
Attempts:
2 left
💡 Hint
Remember that calling parent::__construct() runs the parent's constructor.
Predict Output
intermediate
2:00remaining
Output when child constructor does not call parent constructor
What will be the output when creating an instance of Child if the child constructor does NOT call parent::__construct()?
PHP
<?php
class ParentClass {
    public function __construct() {
        echo "Parent constructor called.";
    }
}

class Child extends ParentClass {
    public function __construct() {
        echo "Child constructor called.";
    }
}

new Child();
?>
AChild constructor called.
BParent constructor called. Child constructor called.
CParent constructor called.
DFatal error: Cannot call parent constructor
Attempts:
2 left
💡 Hint
If the child constructor does not call the parent constructor, the parent's constructor code does not run.
🔧 Debug
advanced
2:00remaining
Identify the error in constructor inheritance
What error will this PHP code produce when creating an instance of Child?
PHP
<?php
class ParentClass {
    public function __construct($name) {
        echo "Hello, $name";
    }
}

class Child extends ParentClass {
    public function __construct() {
        parent::__construct();
        echo " from Child.";
    }
}

new Child();
?>
AHello, from Child.
BChild constructor called without error.
CFatal error: Call to undefined method parent::__construct()
DFatal error: Uncaught ArgumentCountError: Too few arguments to function ParentClass::__construct()
Attempts:
2 left
💡 Hint
Check if the parent constructor requires any arguments and if they are provided.
📝 Syntax
advanced
2:00remaining
Syntax error in constructor inheritance
Which option contains a syntax error in the constructor inheritance code?
PHP
<?php
class ParentClass {
    public function __construct() {
        echo "Parent";
    }
}

class Child extends ParentClass {
    public function __construct() {
        parent::__construct()
        echo "Child";
    }
}

new Child();
?>
AMissing semicolon after parent::__construct() call
BMissing parentheses in parent::__construct call
CMissing function keyword before __construct
DMissing class keyword before Child
Attempts:
2 left
💡 Hint
Check the line where parent::__construct() is called.
🚀 Application
expert
3:00remaining
Determine the final output with multiple inheritance levels and constructor calls
Given the following PHP code with three classes, what is the output when creating an instance of GrandChild?
PHP
<?php
class Base {
    public function __construct() {
        echo "Base ";
    }
}

class ParentClass extends Base {
    public function __construct() {
        parent::__construct();
        echo "Parent ";
    }
}

class GrandChild extends ParentClass {
    public function __construct() {
        parent::__construct();
        echo "GrandChild";
    }
}

new GrandChild();
?>
ABase GrandChild Parent
BGrandChild Parent Base
CBase Parent GrandChild
DParent Base GrandChild
Attempts:
2 left
💡 Hint
Each constructor calls its parent's constructor first, so the output order follows the inheritance chain from base to child.