0
0
PHPprogramming~10 mins

Why interfaces are needed in PHP - Test Your Understanding

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'
ACar
BDrive
CStartable
DVehicle
Attempts:
3 left
💡 Hint
Common Mistakes
Using a class name instead of an interface name.
Using a method name as the interface name.
2fill in blank
medium

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

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'
Aextends
Buses
Cimplements
Dinherits
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'extends' instead of 'implements' for interfaces.
Using 'uses' or 'inherits' which are not valid keywords here.
3fill in blank
hard

Fix the error in the code by completing the blank with the correct keyword to declare an interface.

PHP
<?php
[1] Vehicle {
    public function start();
}
?>
Drag options to blanks, or click blank then click option'
Ainterface
Bclass
Ctrait
Dabstract
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'class' keyword to declare an interface.
Using 'abstract' or 'trait' which are different concepts.
4fill in blank
hard

Fill both blanks to complete the code that shows why interfaces are needed by enforcing a method in multiple classes.

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

class FileLogger implements Logger {
    public function [2]() {
        echo "Logging to a file.";
    }
}
?>
Drag options to blanks, or click blank then click option'
Alog
Bstart
Cwrite
Dprint
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in interface and class.
Choosing unrelated method names like 'start' or 'print'.
5fill in blank
hard

Fill all three blanks to complete the code that uses interfaces to enforce multiple classes to have a common method and demonstrate polymorphism.

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

class Circle implements Shape {
    public function [2]() {
        return "Drawing Circle";
    }
}

class Square implements Shape {
    public function [3]() {
        return "Drawing Square";
    }
}
?>
Drag options to blanks, or click blank then click option'
Adraw
Bstart
Drender
Attempts:
3 left
💡 Hint
Common Mistakes
Using different method names in classes and interface.
Choosing unrelated method names like 'start' or 'render'.