Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q4 of 15
PHP - Interfaces and Traits
What will be the output of this PHP code?
interface Logger { public function log(string $msg); }
class FileLogger implements Logger {
public function log(string $msg) { echo "Log: $msg"; }
}
$logger = new FileLogger();
$logger->log('Test');
ALog: Test
BError: Interface method not implemented
CNo output
DFatal error: Cannot instantiate interface
Step-by-Step Solution
Solution:
  1. Step 1: Understand interface implementation

    FileLogger implements Logger and defines the log method correctly.
  2. Step 2: Trace the method call

    Calling log('Test') prints "Log: Test" as per the method's echo statement.
  3. Final Answer:

    Log: Test -> Option A
  4. Quick Check:

    Interface method implemented = Output printed [OK]
Quick Trick: Implemented interface methods run as defined [OK]
Common Mistakes:
  • Expecting errors if method is implemented
  • Confusing interface with abstract class instantiation
  • Thinking no output occurs without return

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes