0
0
PHPprogramming~20 mins

Interface vs abstract class vs trait in PHP - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of Interface, Abstract Class, and Trait
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of using interface and abstract class methods
What will be the output of this PHP code?
PHP
<?php
interface Flyer {
    public function fly();
}
abstract class Bird {
    abstract public function sing();
    public function walk() {
        echo "Walking\n";
    }
}
class Sparrow extends Bird implements Flyer {
    public function fly() {
        echo "Flying\n";
    }
    public function sing() {
        echo "Singing\n";
    }
}
$sparrow = new Sparrow();
$sparrow->walk();
$sparrow->fly();
$sparrow->sing();
?>
A
Walking
Flying
Singing
B
Flying
Walking
Singing
C
Singing
Walking
Flying
D
Walking
Singing
Flying
Attempts:
2 left
💡 Hint
Remember the order of method calls in the code.
🧠 Conceptual
intermediate
1:30remaining
Key difference between interface and abstract class
Which statement correctly describes a key difference between an interface and an abstract class in PHP?
AAn interface can have method implementations, but an abstract class cannot.
BAn abstract class can have properties and method implementations, but an interface cannot have properties or method implementations.
CAn interface can have properties, but an abstract class cannot.
DBoth interface and abstract class can be instantiated directly.
Attempts:
2 left
💡 Hint
Think about what you can put inside an abstract class versus an interface.
Predict Output
advanced
2:00remaining
Trait method conflict resolution output
What will be the output of this PHP code using traits with conflicting method names?
PHP
<?php
trait A {
    public function hello() {
        echo "Hello from A\n";
    }
}
trait B {
    public function hello() {
        echo "Hello from B\n";
    }
}
class MyClass {
    use A, B {
        B::hello insteadof A;
        A::hello as helloFromA;
    }
}
$obj = new MyClass();
$obj->hello();
$obj->helloFromA();
?>
A
Hello from B
Hello from B
B
Hello from A
Hello from B
C
Hello from A
Hello from A
D
Hello from B
Hello from A
Attempts:
2 left
💡 Hint
Look at which trait's method is used instead of the other and which is aliased.
🔧 Debug
advanced
2:00remaining
Identify the error in trait usage
What error will this PHP code produce?
PHP
<?php
trait Logger {
    public function log(string $msg) {
        echo $msg . "\n";
    }
}
class User {
    use Logger;
    public function log(int $msg) {
        echo "User log: " . $msg . "\n";
    }
}
$user = new User();
$user->log("Hello");
?>
AOutput: Hello
BOutput: User log: Hello
CFatal error: Declaration of User::log(int $msg) must be compatible with Logger::log(string $msg)
DFatal error: Trait method Logger::log has not been applied, because there is a collision with User::log
Attempts:
2 left
💡 Hint
Check method signatures when overriding trait methods.
🚀 Application
expert
2:30remaining
Choosing the right structure for code reuse
You want to create reusable logging functionality that can be added to multiple unrelated classes without forcing them into a common parent class. Which PHP feature is best suited for this?
AUse a trait containing the logging methods and include it in all classes.
BUse an abstract class with the logging methods and extend it in all classes.
CUse a final class with static logging methods and call them from all classes.
DUse an interface declaring logging methods and implement it in all classes.
Attempts:
2 left
💡 Hint
Think about how to add the same code to many classes without inheritance.