0
0
PHPprogramming~20 mins

Parent keyword behavior in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Parent Keyword in PHP
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of parent:: method call in inheritance
What is the output of the following PHP code?
PHP
<?php
class A {
    public function greet() {
        return "Hello from A";
    }
}
class B extends A {
    public function greet() {
        return parent::greet() . " and B";
    }
}
$b = new B();
echo $b->greet();
?>
AHello from A and B
BHello from B
CHello from A
DFatal error: Cannot access parent::greet()
Attempts:
2 left
💡 Hint
Remember that parent:: calls the method from the parent class.
Predict Output
intermediate
2:00remaining
Using parent:: in constructor
What will be the output of this PHP code?
PHP
<?php
class ParentClass {
    public function __construct() {
        echo "Parent constructor called. ";
    }
}
class ChildClass extends ParentClass {
    public function __construct() {
        parent::__construct();
        echo "Child constructor called.";
    }
}
new ChildClass();
?>
AChild constructor called.
BParent constructor called. Child constructor called.
CParent constructor called.
DFatal error: Cannot call parent constructor
Attempts:
2 left
💡 Hint
Check how constructors are called in inheritance.
Predict Output
advanced
2:00remaining
Output when parent method is private
What happens when a child class tries to call a private parent method using parent:: ?
PHP
<?php
class Base {
    private function secret() {
        return "Secret from Base";
    }
    public function reveal() {
        return $this->secret();
    }
}
class Derived extends Base {
    public function secret() {
        return "Secret from Derived";
    }
    public function revealSecret() {
        return parent::secret();
    }
}
$d = new Derived();
echo $d->reveal();
echo " - ";
echo $d->secret();
echo " - ";
echo $d->revealSecret();
?>
AFatal error - Secret from Derived - Secret from Base
BSecret from Base - Secret from Derived - Secret from Base
CSecret from Base - Secret from Derived - Fatal error
DSecret from Base - Fatal error - Secret from Derived
Attempts:
2 left
💡 Hint
Private methods are not accessible via parent:: in child classes.
Predict Output
advanced
2:00remaining
parent:: with static methods and late static binding
What is the output of this PHP code involving static methods and parent::?
PHP
<?php
class Alpha {
    public static function who() {
        return "Alpha";
    }
    public static function test() {
        return static::who();
    }
}
class Beta extends Alpha {
    public static function who() {
        return "Beta";
    }
    public static function test() {
        return parent::test();
    }
}
echo Beta::test();
?>
ABetaBeta
BAlpha
CFatal error: Cannot access parent::test()
DBeta
Attempts:
2 left
💡 Hint
Remember that static:: uses late static binding to call the method in the called class.
Predict Output
expert
3:00remaining
Complex parent:: calls with traits and multiple inheritance
What is the output of this PHP code involving traits and parent:: calls?
PHP
<?php
trait T {
    public function say() {
        return "Trait says hi";
    }
}
class Base {
    public function say() {
        return "Base says hi";
    }
}
class Child extends Base {
    use T;
    public function say() {
        return parent::say() . " and " . T::say();
    }
}
$c = new Child();
echo $c->say();
?>
AFatal error: Cannot access trait method T::say() directly
BBase says hi and Trait says hi
CTrait says hi and Base says hi
DBase says hi
Attempts:
2 left
💡 Hint
Traits methods cannot be called statically like T::say() inside an instance method.