0
0
PHPprogramming~10 mins

Interface declaration and implementation 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 declare an interface named Vehicle.

PHP
<?php
interface [1] {
    public function start();
}
?>
Drag options to blanks, or click blank then click option'
AStartable
BCar
CDrive
DVehicle
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name instead of an interface name.
Forgetting to use the keyword 'interface'.
2fill in blank
medium

Complete the code to implement the Vehicle interface in the Car class.

PHP
<?php
interface Vehicle {
    public function start();
}

class Car [1] Vehicle {
    public function start() {
        echo "Car started";
    }
}
?>
Drag options to blanks, or click blank then click option'
Auses
Bextends
Cimplements
Dinherits
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extends' instead of 'implements' for interfaces.
Forgetting to implement all interface methods.
3fill in blank
hard

Fix the error in the code by completing the method signature in the Car class.

PHP
<?php
interface Vehicle {
    public function start();
}

class Car implements Vehicle {
    public function [1] {
        echo "Car started";
    }
}
?>
Drag options to blanks, or click blank then click option'
Astart()
Bbegin()
CStart()
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting parentheses in the method declaration.
Using a different method name than declared in the interface.
4fill in blank
hard

Fill both blanks to declare an interface and a class that implements it with a method.

PHP
<?php
interface [1] {
    public function [2]();
}

class Bike implements Vehicle {
    public function start() {
        echo "Bike started";
    }
}
?>
Drag options to blanks, or click blank then click option'
AVehicle
BCar
Cstart
Drun
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in interface and class.
Mismatching interface and class names.
5fill in blank
hard

Fill all three blanks to create an interface with two methods and a class implementing both.

PHP
<?php
interface [1] {
    public function [2]();
    public function [3]();
}

class Plane implements Flyable {
    public function takeOff() {
        echo "Plane taking off";
    }
    public function land() {
        echo "Plane landing";
    }
}
?>
Drag options to blanks, or click blank then click option'
AFlyable
BtakeOff
Cland
Dstart
Attempts:
3 left
💡 Hint
Common Mistakes
Not implementing all interface methods in the class.
Using method names in the interface that don't match the class.