Bird
Raised Fist0
PHPprogramming~10 mins

Why inheritance is needed in PHP - Test Your Understanding

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a class that inherits from another class.

PHP
<?php
class Animal {
    public function sound() {
        echo "Some sound";
    }
}

class Dog extends [1] {
}

$dog = new Dog();
$dog->sound();
?>
Drag options to blanks, or click blank then click option'
ADog
BAnimal
CSound
DPet
Attempts:
3 left
💡 Hint
Common Mistakes
Using the child class name instead of the parent class name after extends.
Forgetting to use the extends keyword.
2fill in blank
medium

Complete the code to override the method in the child class.

PHP
<?php
class Animal {
    public function sound() {
        echo "Some sound";
    }
}

class Cat extends Animal {
    public function [1]() {
        echo "Meow";
    }
}

$cat = new Cat();
$cat->sound();
?>
Drag options to blanks, or click blank then click option'
Asound
Bnoise
Cvoice
DmakeSound
Attempts:
3 left
💡 Hint
Common Mistakes
Changing the method name so it does not override the parent method.
Forgetting to define the method in the child class.
3fill in blank
hard

Fix the error in the code to call the parent class method inside the child class method.

PHP
<?php
class Bird {
    public function sound() {
        echo "Chirp";
    }
}

class Parrot extends Bird {
    public function sound() {
        [1]::sound();
        echo " and Talk";
    }
}

$parrot = new Parrot();
$parrot->sound();
?>
Drag options to blanks, or click blank then click option'
ABird
Bself
Cparent
D$this
Attempts:
3 left
💡 Hint
Common Mistakes
Using self or class name instead of parent to call the parent method.
Using $this which calls the current class method causing recursion.
4fill in blank
hard

Fill both blanks to create a class that inherits and adds a new method.

PHP
<?php
class Vehicle {
    public function start() {
        echo "Starting vehicle";
    }
}

class Car [1] Vehicle {
    public function [2]() {
        echo "Driving car";
    }
}

$car = new Car();
$car->start();
$car->drive();
?>
Drag options to blanks, or click blank then click option'
Aextends
Bimplements
Cdrive
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using implements instead of extends for class inheritance.
Naming the new method incorrectly.
5fill in blank
hard

Fill all three blanks to create a class that inherits, overrides a method, and calls the parent method.

PHP
<?php
class Computer {
    public function boot() {
        echo "Booting computer";
    }
}

class Laptop [1] Computer {
    public function [2]() {
        [3]::boot();
        echo " with battery";
    }
}

$laptop = new Laptop();
$laptop->boot();
?>
Drag options to blanks, or click blank then click option'
Aextends
Bboot
Cparent
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong keywords for inheritance.
Not matching the method name when overriding.
Calling the parent method incorrectly.

Practice

(1/5)
1. Why do we use inheritance in PHP classes?
easy
A. To reuse code from an existing class and avoid duplication
B. To make the program run faster
C. To create variables without values
D. To write code without functions

Solution

  1. Step 1: Understand the purpose of inheritance

    Inheritance allows a new class to use properties and methods from an existing class, so we don't repeat code.
  2. Step 2: Compare options with inheritance purpose

    Only To reuse code from an existing class and avoid duplication correctly states that inheritance helps reuse code and avoid duplication.
  3. Final Answer:

    To reuse code from an existing class and avoid duplication -> Option A
  4. Quick Check:

    Inheritance = Code reuse [OK]
Hint: Inheritance means reusing code from another class [OK]
Common Mistakes:
  • Thinking inheritance makes code run faster
  • Confusing inheritance with variable declaration
  • Believing inheritance removes need for functions
2. Which of the following is the correct syntax to make class Car inherit from class Vehicle in PHP?
easy
A. class Car extends Vehicle {}
B. class Car inherits Vehicle {}
C. class Car Vehicle {}
D. class Car : Vehicle {}

Solution

  1. Step 1: Recall PHP inheritance syntax

    In PHP, the keyword extends is used to inherit from another class.
  2. Step 2: Match syntax with options

    Only class Car extends Vehicle {} uses extends correctly to inherit from Vehicle.
  3. Final Answer:

    class Car extends Vehicle {} -> Option A
  4. Quick Check:

    Inheritance keyword = extends [OK]
Hint: Use 'extends' keyword to inherit in PHP [OK]
Common Mistakes:
  • Using 'inherits' instead of 'extends'
  • Omitting 'extends' keyword
  • Using ':' like other languages
3. What will be the output of this PHP code?
class Animal {
  public function sound() {
    return "Some sound";
  }
}
class Dog extends Animal {
  public function sound() {
    return "Bark";
  }
}
$dog = new Dog();
echo $dog->sound();
medium
A. Some sound
B. Error: Method not found
C. Bark
D. No output

Solution

  1. Step 1: Understand method overriding in inheritance

    The Dog class inherits from Animal but overrides the sound() method to return "Bark".
  2. Step 2: Check which method is called

    When calling $dog->sound(), the overridden method in Dog runs, returning "Bark".
  3. Final Answer:

    Bark -> Option C
  4. Quick Check:

    Overridden method runs = Bark [OK]
Hint: Child method overrides parent method output [OK]
Common Mistakes:
  • Expecting parent method output
  • Thinking method not found error occurs
  • Ignoring method overriding
4. Identify the error in this PHP code using inheritance:
class ParentClass {
  public function greet() {
    return "Hello";
  }
}
class ChildClass ParentClass {
  public function greet() {
    return "Hi";
  }
}
medium
A. Method greet() cannot be overridden
B. Missing 'extends' keyword before ParentClass
C. Class names cannot be ParentClass or ChildClass
D. No error, code is correct

Solution

  1. Step 1: Check inheritance syntax

    In PHP, to inherit, the child class must use the keyword extends before the parent class name.
  2. Step 2: Identify missing keyword

    The code misses extends in class ChildClass ParentClass, causing a syntax error.
  3. Final Answer:

    Missing 'extends' keyword before ParentClass -> Option B
  4. Quick Check:

    Inheritance requires 'extends' keyword [OK]
Hint: Inheritance needs 'extends' keyword in class declaration [OK]
Common Mistakes:
  • Omitting 'extends' keyword
  • Thinking method override is disallowed
  • Assuming class names cause error
5. You have a base class Employee with a method getDetails(). You want to create a Manager class that adds a new method getTeamSize() but still uses getDetails() from Employee. How should you use inheritance to achieve this?
hard
A. Use two separate classes without any relation
B. Make Employee extend Manager and add getTeamSize() method
C. Create Manager without extending Employee and copy getDetails() code
D. Make Manager extend Employee and add getTeamSize() method

Solution

  1. Step 1: Understand inheritance for code reuse

    To reuse getDetails() from Employee, Manager should inherit from Employee.
  2. Step 2: Add new method in child class

    Then, Manager can add its own method getTeamSize() without rewriting getDetails().
  3. Final Answer:

    Make Manager extend Employee and add getTeamSize() method -> Option D
  4. Quick Check:

    Child class extends parent to add features [OK]
Hint: Child class extends parent to add new methods [OK]
Common Mistakes:
  • Making parent extend child (wrong direction)
  • Copying code instead of inheriting
  • Not relating classes at all
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard