0
0
PHPprogramming~10 mins

Interface vs abstract class vs trait in PHP - Interactive 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 Logger.

PHP
<?php
interface [1] {
    public function log(string $msg);
}
?>
Drag options to blanks, or click blank then click option'
ALogTrait
BLogger
CLogInterface
DAbstractLogger
Attempts:
3 left
💡 Hint
Common Mistakes
Using trait or abstract class names instead of interface name.
2fill in blank
medium

Complete the code to declare an abstract class named Vehicle.

PHP
<?php
abstract class [1] {
    abstract public function startEngine();
}
?>
Drag options to blanks, or click blank then click option'
ADrive
BCar
CVehicle
DEngine
Attempts:
3 left
💡 Hint
Common Mistakes
Using specific class names instead of a general abstract class name.
3fill in blank
hard

Fix the error in the trait declaration by completing the code.

PHP
<?php
trait [1] {
    public function sayHello() {
        echo "Hello!";
    }
}
?>
Drag options to blanks, or click blank then click option'
AGreetingTrait
BHelloInterface
CAbstractGreeting
DSayHelloClass
Attempts:
3 left
💡 Hint
Common Mistakes
Using interface or class names instead of trait name.
4fill in blank
hard

Fill both blanks to implement the Logger interface and use the GreetingTrait in the class.

PHP
<?php
class User implements [1] {
    use [2];
    public function log(string $msg) {
        echo $msg;
    }
}
?>
Drag options to blanks, or click blank then click option'
ALogger
BGreetingTrait
CLogTrait
DAbstractLogger
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing trait names with interface names.
Using wrong keywords for implementation or usage.
5fill in blank
hard

Fill all three blanks to declare an abstract class with an abstract method and use a trait inside it.

PHP
<?php
abstract class [1] {
    use [2];
    abstract public function [3]();
}
?>
Drag options to blanks, or click blank then click option'
AVehicle
BGreetingTrait
CstartEngine
DLogger
Attempts:
3 left
💡 Hint
Common Mistakes
Using interface names instead of class names.
Confusing method names with trait or class names.