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?
trait Logger { function log() { echo 'Logging'; } } class User { use Logger; } $u = new User(); $u->log();
ALogging
BError: Method log() not found
CNo output
DFatal error: Trait not found
Step-by-Step Solution
Solution:
  1. Step 1: Understand trait method availability

    The trait Logger defines method log(), which is included in User class via use Logger.
  2. Step 2: Calling log() on User instance

    Since User uses Logger, calling $u->log() executes Logger::log(), printing 'Logging'.
  3. Final Answer:

    Logging -> Option A
  4. Quick Check:

    Trait method call output = Logging [OK]
Quick Trick: Trait methods become class methods after use [OK]
Common Mistakes:
  • Expecting error due to trait usage
  • Thinking trait methods are inaccessible
  • Confusing traits with interfaces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes