0
0
PHPprogramming~20 mins

Why inheritance is needed in PHP - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Inheritance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of PHP inheritance example
What will be the output of the following PHP code demonstrating inheritance?
PHP
<?php
class Animal {
    public function sound() {
        return "Some sound";
    }
}

class Dog extends Animal {
    public function sound() {
        return "Bark";
    }
}

$dog = new Dog();
echo $dog->sound();
?>
ASome sound
BFatal error: Cannot redeclare method
CBark
DNo output
Attempts:
2 left
💡 Hint
Look at which class method is called when using the Dog object.
🧠 Conceptual
intermediate
1:30remaining
Why use inheritance in PHP?
Why is inheritance needed in PHP programming?
ATo reuse code and create a hierarchy of classes
BTo make code run faster by skipping functions
CTo prevent any class from being used more than once
DTo avoid writing any functions in classes
Attempts:
2 left
💡 Hint
Think about how inheritance helps avoid repeating code.
🔧 Debug
advanced
2:00remaining
Identify the error in this inheritance code
What error will this PHP code produce?
PHP
<?php
class Vehicle {
    public function start() {
        echo "Vehicle started";
    }
}

class Car extends Vehicle {
    public function start() {
        echo "Car started";
    }
}

$car = new Vehicle();
$car->start();
?>
AOutputs 'Car started'
BFatal error: Call to undefined method
CFatal error: Cannot instantiate abstract class
DOutputs 'Vehicle started'
Attempts:
2 left
💡 Hint
Check which class is instantiated and which method is called.
📝 Syntax
advanced
1:30remaining
Which code correctly defines inheritance in PHP?
Which of the following PHP code snippets correctly shows inheritance?
Aclass Child inherits Parent {}
Bclass Child extends Parent {}
Cclass Child : Parent {}
Dclass Child -> Parent {}
Attempts:
2 left
💡 Hint
Remember the keyword PHP uses for inheritance.
🚀 Application
expert
2:30remaining
How many methods does the object have access to?
Given the following PHP code, how many methods can the $sportsCar object call?
PHP
<?php
class Vehicle {
    public function start() { return "Vehicle started"; }
    public function stop() { return "Vehicle stopped"; }
}

class Car extends Vehicle {
    public function openDoor() { return "Door opened"; }
}

class SportsCar extends Car {
    public function turboBoost() { return "Turbo boost activated"; }
}

$sportsCar = new SportsCar();
?>
A4
B3
C2
D5
Attempts:
2 left
💡 Hint
Count all methods from SportsCar and its parent classes accessible to the object.