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
intermediate2: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(); ?>
Attempts:
2 left
💡 Hint
Remember that parent:: calls the method from the parent class.
✗ Incorrect
The method greet() in class B calls parent::greet(), which runs the greet() method from class A, returning "Hello from A". Then it concatenates " and B". So the output is "Hello from A and B".
❓ Predict Output
intermediate2: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(); ?>
Attempts:
2 left
💡 Hint
Check how constructors are called in inheritance.
✗ Incorrect
The ChildClass constructor calls parent::__construct(), which runs the ParentClass constructor printing "Parent constructor called. ". Then it prints "Child constructor called.". So the full output is "Parent constructor called. Child constructor called.".
❓ Predict Output
advanced2: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(); ?>
Attempts:
2 left
💡 Hint
Private methods are not accessible via parent:: in child classes.
✗ Incorrect
Base::reveal() calls $this->secret(). Since secret() is private in Base and private methods cannot be overridden, it calls Base's private secret(), returning "Secret from Base". Derived's public secret() is a separate method, so $d->secret() returns "Secret from Derived". parent::secret() from Derived cannot access Base's private method, causing a fatal error. Output: "Secret from Base - Secret from Derived - Fatal error".
❓ Predict Output
advanced2: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(); ?>
Attempts:
2 left
💡 Hint
Remember that static:: uses late static binding to call the method in the called class.
✗ Incorrect
Beta::test() calls parent::test(), which is Alpha::test(). Alpha::test() calls static::who(), which uses late static binding and calls Beta::who(), returning "Beta". So the output is "Beta".
❓ Predict Output
expert3: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(); ?>
Attempts:
2 left
💡 Hint
Traits methods cannot be called statically like T::say() inside an instance method.
✗ Incorrect
Inside Child::say(), parent::say() correctly calls Base::say(). However, T::say() is a trait method and cannot be called statically like that. This causes a fatal error. The correct way would be to call $this->say() carefully or rename methods to avoid conflict.