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
intermediate2: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(); ?>
Attempts:
2 left
💡 Hint
Remember the order of method calls in the code.
✗ Incorrect
The code calls walk(), then fly(), then sing() in that order, so the output matches option A.
🧠 Conceptual
intermediate1:30remaining
Key difference between interface and abstract class
Which statement correctly describes a key difference between an interface and an abstract class in PHP?
Attempts:
2 left
💡 Hint
Think about what you can put inside an abstract class versus an interface.
✗ Incorrect
Abstract classes can have properties and method implementations. Interfaces cannot have properties or method implementations.
❓ Predict Output
advanced2: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(); ?>
Attempts:
2 left
💡 Hint
Look at which trait's method is used instead of the other and which is aliased.
✗ Incorrect
The code uses B's hello() method instead of A's, but also aliases A's hello() as helloFromA. So calling hello() prints B's message, and helloFromA() prints A's message.
🔧 Debug
advanced2: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"); ?>
Attempts:
2 left
💡 Hint
Check method signatures when overriding trait methods.
✗ Incorrect
The User class tries to override the trait method log() but changes the parameter type from string to int, which is incompatible and causes a fatal error.
🚀 Application
expert2: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?
Attempts:
2 left
💡 Hint
Think about how to add the same code to many classes without inheritance.
✗ Incorrect
Traits allow code reuse by including methods directly into classes without inheritance, making them ideal for adding logging to unrelated classes.