Bird
Raised Fist0
PHPprogramming~10 mins

Method overriding 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 override the method greet in the child class.

PHP
<?php
class ParentClass {
    public function greet() {
        echo "Hello from parent";
    }
}

class ChildClass extends ParentClass {
    public function greet() {
        [1]
    }
}

$child = new ChildClass();
$child->greet();
?>
Drag options to blanks, or click blank then click option'
Aparent::greet();
Bprint("Hello from parent");
Creturn "Hello from child";
Decho "Hello from child";
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent::greet() instead of overriding the method.
Using return instead of echo for output.
Not changing the message inside the child method.
2fill in blank
medium

Complete the code to call the parent class method inside the overridden method.

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

class Dog extends Animal {
    public function sound() {
        [1]
        echo " and bark";
    }
}

$dog = new Dog();
$dog->sound();
?>
Drag options to blanks, or click blank then click option'
AAnimal::sound();
Bparent::sound();
Cself::sound();
Decho "Some sound";
Attempts:
3 left
💡 Hint
Common Mistakes
Using self::sound() which calls the current class method causing recursion.
Using Animal::sound() which is not recommended for calling parent methods.
Echoing the string directly instead of calling the parent method.
3fill in blank
hard

Fix the error in the overridden method to correctly call the parent method and add extra output.

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

class Car extends Vehicle {
    public function start() {
        [1]
        echo " and car is ready";
    }
}

$car = new Car();
$car->start();
?>
Drag options to blanks, or click blank then click option'
Aparent::start();
BVehicle::start();
Cparent->start();
Dself::start();
Attempts:
3 left
💡 Hint
Common Mistakes
Using parent->start() which is invalid syntax.
Using self::start() causing infinite recursion.
Calling Vehicle::start() directly which is less flexible.
4fill in blank
hard

Fill both blanks to override the method and call the parent method with extra output.

PHP
<?php
class Printer {
    public function printMessage() {
        echo "Printing...";
    }
}

class ColorPrinter extends Printer {
    public function printMessage() {
        [1];
        echo [2];
    }
}

$printer = new ColorPrinter();
$printer->printMessage();
?>
Drag options to blanks, or click blank then click option'
Aparent::printMessage()
B" in color"
C" in black and white"
DprintMessage()
Attempts:
3 left
💡 Hint
Common Mistakes
Not calling the parent method before echoing extra text.
Using printMessage() without parent:: causing recursion.
Echoing the wrong string.
5fill in blank
hard

Fill all three blanks to override the method, call the parent method, and add a custom message.

PHP
<?php
class Writer {
    public function write() {
        echo "Writing text";
    }
}

class FancyWriter extends Writer {
    public function write() {
        echo [1];
        [2];
        echo [3];
    }
}

$fancy = new FancyWriter();
$fancy->write();
?>
Drag options to blanks, or click blank then click option'
A"Fancy: "
Bparent::write()
C" with style"
Dwrite()
Attempts:
3 left
💡 Hint
Common Mistakes
Calling write() instead of parent::write() causing recursion.
Not echoing the prefix or suffix strings.
Using incorrect syntax for calling parent method.

Practice

(1/5)
1. What does method overriding allow a child class to do in PHP?
easy
A. Change the behavior of a method inherited from the parent class
B. Create a new method with a different name
C. Prevent the parent class from being instantiated
D. Automatically call all parent methods

Solution

  1. Step 1: Understand method overriding concept

    Method overriding means a child class uses the same method name as the parent to change its behavior.
  2. Step 2: Identify what overriding does

    It replaces or modifies the parent's method behavior in the child class.
  3. Final Answer:

    Change the behavior of a method inherited from the parent class -> Option A
  4. Quick Check:

    Method overriding = change parent method behavior [OK]
Hint: Overriding means same method name, new behavior [OK]
Common Mistakes:
  • Thinking overriding creates new methods with different names
  • Confusing overriding with inheritance itself
  • Believing overriding prevents parent instantiation
2. Which of the following is the correct syntax to override a method named greet in a child class in PHP?
easy
A. function greetChild() { /* new code */ }
B. function greet() { /* new code */ }
C. override function greet() { /* new code */ }
D. parent::greet() { /* new code */ }

Solution

  1. Step 1: Recall method overriding syntax

    To override, define a method with the same name in the child class.
  2. Step 2: Check options for correct syntax

    Only function greet() { /* new code */ } defines function greet() correctly without extra keywords.
  3. Final Answer:

    function greet() { /* new code */ } -> Option B
  4. Quick Check:

    Same method name in child = override [OK]
Hint: Use same method name in child class to override [OK]
Common Mistakes:
  • Adding 'override' keyword which PHP does not use
  • Changing method name to something else
  • Trying to call parent method syntax incorrectly
3. What will be the output of this PHP code?
class ParentClass {
  function sayHello() {
    echo "Hello from Parent";
  }
}
class ChildClass extends ParentClass {
  function sayHello() {
    echo "Hello from Child";
  }
}
$obj = new ChildClass();
$obj->sayHello();
medium
A. Hello from ParentChild
B. Hello from Parent
C. Hello from Child
D. Error: Method not found

Solution

  1. Step 1: Identify method overriding in ChildClass

    ChildClass defines sayHello() which overrides ParentClass method.
  2. Step 2: Check which method is called

    Creating ChildClass object calls ChildClass's sayHello(), printing "Hello from Child".
  3. Final Answer:

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

    Child method overrides parent = "Hello from Child" [OK]
Hint: Child method runs if overridden, not parent [OK]
Common Mistakes:
  • Expecting parent method output instead of child
  • Thinking both outputs print together
  • Assuming error due to method name clash
4. Find the error in this PHP code that tries to override a method:
class Base {
  function show() {
    echo "Base";
  }
}
class Derived extends Base {
  function show() {
    parent.show();
    echo " Derived";
  }
}
$obj = new Derived();
$obj->show();
medium
A. Class Derived must declare show() as public
B. Cannot override methods in PHP
C. Missing return statement in show()
D. Incorrect syntax to call parent method; should use parent::show()

Solution

  1. Step 1: Identify how parent method is called

    The code uses parent.show() which is invalid syntax in PHP.
  2. Step 2: Correct syntax for calling parent method

    PHP requires parent::show() to call the parent's method inside child.
  3. Final Answer:

    Incorrect syntax to call parent method; should use parent::show() -> Option D
  4. Quick Check:

    Call parent method with parent::method() [OK]
Hint: Use parent::method() to call parent method inside child [OK]
Common Mistakes:
  • Using dot (.) instead of :: to call parent method
  • Thinking overriding is not allowed
  • Forgetting to call parent method when needed
5. Given these classes, what will be the output?
class Animal {
  function sound() {
    echo "Some sound";
  }
}
class Dog extends Animal {
  function sound() {
    parent::sound();
    echo " and Bark";
  }
}
class Puppy extends Dog {
  function sound() {
    echo "Yip";
  }
}
$pet = new Puppy();
$pet->sound();
hard
A. Yip
B. Some sound and Bark and Yip
C. Some sound and Bark
D. Error: Cannot override multiple times

Solution

  1. Step 1: Trace method calls for Puppy object

    Puppy overrides sound() and only echoes "Yip" without calling parent.
  2. Step 2: Understand overriding chain

    Since Puppy method does not call parent::sound(), Dog and Animal methods are not executed.
  3. Final Answer:

    Yip -> Option A
  4. Quick Check:

    Child override without parent call = only child output [OK]
Hint: If child method skips parent call, only child output shows [OK]
Common Mistakes:
  • Assuming all parent methods run automatically
  • Expecting combined outputs without explicit parent calls
  • Thinking multiple overrides cause errors
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