0
0
PHPprogramming~10 mins

Extending classes 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 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.