0
0
PHPprogramming~10 mins

Method overriding in PHP - Interactive Code Practice

Choose your learning style9 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.