Bird
Raised Fist0
PHPprogramming~10 mins

Extending classes in PHP - Interactive Code Practice

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

Complete the code to make class B inherit from class A.

PHP
<?php
class A {
    public function greet() {
        return "Hello from A";
    }
}

class B [1] A {
}

$b = new B();
echo $b->greet();
?>
Drag options to blanks, or click blank then click option'
Aextends
Bimplements
Cinherits
Duses
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'implements' which is for interfaces, not classes.
Using 'inherits' which is not a PHP keyword.
Using 'uses' which is for traits.
2fill in blank
medium

Complete the code to call the parent class constructor inside the child class constructor.

PHP
<?php
class ParentClass {
    public function __construct() {
        echo "Parent constructor called\n";
    }
}

class ChildClass extends ParentClass {
    public function __construct() {
        [1];
        echo "Child constructor called\n";
    }
}

new ChildClass();
?>
Drag options to blanks, or click blank then click option'
Aparent::__construct()
Bself::__construct()
CChildClass::__construct()
Dthis->__construct()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self::__construct()' which calls the current class constructor and causes recursion.
Using 'this->__construct()' which is invalid syntax in PHP.
Calling the child class constructor inside itself causing infinite loop.
3fill in blank
hard

Fix the error in the code to properly override the greet method and call the parent greet method.

PHP
<?php
class Base {
    public function greet() {
        return "Hello from Base";
    }
}

class Derived extends Base {
    public function greet() {
        return [1] . " and Derived";
    }
}

$d = new Derived();
echo $d->greet();
?>
Drag options to blanks, or click blank then click option'
ABase::greet()
B$this->greet()
Cparent->greet()
Dparent::greet()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'parent->greet()' which is invalid syntax.
Using '$this->greet()' which calls the current method recursively.
Using 'Base::greet()' which requires static context or an instance.
4fill in blank
hard

Fill both blanks to create a child class that overrides a method and calls the parent method inside it.

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

class Dog [1] Animal {
    public function sound() {
        return [2] . " Woof!";
    }
}

$dog = new Dog();
echo $dog->sound();
?>
Drag options to blanks, or click blank then click option'
Aextends
Bimplements
Cparent::sound()
Dself::sound()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'implements' instead of 'extends' for class inheritance.
Using 'self::sound()' which calls the current class method and causes recursion.
5fill in blank
hard

Fill all three blanks to define a child class that extends a parent, overrides a method, and calls the parent's method with additional text.

PHP
<?php
class Vehicle {
    public function description() {
        return "This is a vehicle";
    }
}

class Car [1] Vehicle {
    public function description() {
        return [2] . [3];
    }
}

$car = new Car();
echo $car->description();
?>
Drag options to blanks, or click blank then click option'
Aextends
Bparent::description()
C " and it has 4 wheels"
Dself::description()
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'self::description()' which calls the current method recursively.
Not using 'extends' for inheritance.
Forgetting to concatenate the strings properly.

Practice

(1/5)
1. What keyword is used in PHP to create a child class that inherits from a parent class?
easy
A. extends
B. inherits
C. implements
D. uses

Solution

  1. Step 1: Understand class inheritance in PHP

    PHP uses a specific keyword to let one class inherit properties and methods from another.
  2. Step 2: Identify the correct keyword

    The keyword to extend a class in PHP is extends, not inherits, implements, or uses.
  3. Final Answer:

    extends -> Option A
  4. Quick Check:

    Keyword for inheritance = extends [OK]
Hint: Remember: PHP uses 'extends' to inherit classes [OK]
Common Mistakes:
  • Using 'inherits' instead of 'extends'
  • Confusing 'implements' with 'extends'
  • Using 'uses' keyword incorrectly
2. Which of the following is the correct syntax to extend a class named Animal in PHP?
easy
A. class Dog extends Animal {}
B. class Dog inherits Animal {}
C. class Dog implements Animal {}
D. class Dog use Animal {}

Solution

  1. Step 1: Recall PHP class extension syntax

    To create a child class, PHP uses the syntax: class Child extends Parent {}.
  2. Step 2: Match the syntax with options

    class Dog extends Animal {} matches the correct syntax. The other options use incorrect keywords.
  3. Final Answer:

    class Dog extends Animal {} -> Option A
  4. Quick Check:

    Correct syntax uses 'extends' keyword [OK]
Hint: Use 'extends' keyword exactly after class name [OK]
Common Mistakes:
  • Using 'inherits' instead of 'extends'
  • Confusing 'implements' with 'extends'
  • Using 'use' keyword for inheritance
3. What will be the output of the following PHP code?
class ParentClass {
    public function greet() {
        return "Hello from Parent";
    }
}

class ChildClass extends ParentClass {
    public function greet() {
        return "Hello from Child";
    }
}

$object = new ChildClass();
echo $object->greet();
medium
A. Hello from Parent
B. Fatal error
C. Hello from Child
D. No output

Solution

  1. Step 1: Understand method overriding in child class

    The child class ChildClass overrides the greet() method of the parent class.
  2. Step 2: Determine which method is called

    When calling greet() on an instance of ChildClass, the overridden method in the child class runs.
  3. Final Answer:

    Hello from Child -> Option C
  4. Quick Check:

    Child method overrides parent method [OK]
Hint: Child method overrides parent method with same name [OK]
Common Mistakes:
  • Assuming parent method runs instead of child
  • Expecting syntax error due to override
  • Thinking no output will be printed
4. Identify the error in this PHP code that tries to extend a class:
class Vehicle {
    public function start() {
        return "Starting vehicle";
    }
}

class Car Vehicle {
    public function start() {
        return "Starting car";
    }
}
medium
A. Class Car should not have methods
B. Method start() cannot be overridden
C. Class Vehicle should be abstract
D. Missing 'extends' keyword before Vehicle

Solution

  1. Step 1: Check class extension syntax

    In PHP, to extend a class, the child class must use the extends keyword.
  2. Step 2: Identify the missing keyword

    The code class Car Vehicle {} misses extends between Car and Vehicle.
  3. Final Answer:

    Missing 'extends' keyword before Vehicle -> Option D
  4. Quick Check:

    Extending requires 'extends' keyword [OK]
Hint: Always write 'extends' when inheriting a class [OK]
Common Mistakes:
  • Omitting 'extends' keyword
  • Thinking methods cannot be overridden
  • Confusing abstract class requirement
5. Given the following classes, what will be the output?
class Base {
    public function message() {
        return "Base message";
    }
}

class Derived extends Base {
    public function message() {
        return parent::message() . " and Derived message";
    }
}

class MoreDerived extends Derived {
    public function message() {
        return parent::message() . " plus MoreDerived message";
    }
}

$obj = new MoreDerived();
echo $obj->message();
hard
A. MoreDerived message
B. Base message and Derived message plus MoreDerived message
C. Base message
D. Fatal error due to parent call

Solution

  1. Step 1: Trace method calls through inheritance chain

    MoreDerived::message() calls parent::message(), which is Derived::message().
  2. Step 2: Understand Derived::message() call

    Derived::message() calls parent::message(), which is Base::message(), returning "Base message".
  3. Step 3: Combine returned strings

    Base returns "Base message"; Derived appends " and Derived message"; MoreDerived appends " plus MoreDerived message".
  4. Final Answer:

    Base message and Derived message plus MoreDerived message -> Option B
  5. Quick Check:

    parent:: calls chain methods up inheritance [OK]
Hint: parent:: calls chain up inheritance to combine outputs [OK]
Common Mistakes:
  • Ignoring parent:: calls and output only last method
  • Expecting fatal error from parent calls
  • Thinking only base method runs
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