Challenge - 5 Problems
Constructor Inheritance Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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(); ?>
Attempts:
2 left
💡 Hint
Remember that calling
parent::__construct() runs the parent's constructor.✗ Incorrect
The
Child constructor calls parent::__construct(), so the parent's constructor message prints first, then the child's message prints.❓ Predict Output
intermediate2: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(); ?>
Attempts:
2 left
💡 Hint
If the child constructor does not call the parent constructor, the parent's constructor code does not run.
✗ Incorrect
Since the child constructor overrides the parent's and does not call
parent::__construct(), only the child's message prints.🔧 Debug
advanced2: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(); ?>
Attempts:
2 left
💡 Hint
Check if the parent constructor requires any arguments and if they are provided.
✗ Incorrect
The parent constructor requires one argument
$name, but the child calls it without arguments, causing an ArgumentCountError.📝 Syntax
advanced2: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(); ?>
Attempts:
2 left
💡 Hint
Check the line where
parent::__construct() is called.✗ Incorrect
The line calling
parent::__construct() is missing a semicolon at the end, causing a syntax error.🚀 Application
expert3: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(); ?>
Attempts:
2 left
💡 Hint
Each constructor calls its parent's constructor first, so the output order follows the inheritance chain from base to child.
✗ Incorrect
The
GrandChild constructor calls ParentClass::__construct(), which calls Base::__construct(). So the output prints in order: Base, Parent, GrandChild.