Bird
0
0

Identify the error in this PHP code:

medium📝 Debug Q7 of 15
PHP - Interfaces and Traits
Identify the error in this PHP code:
trait Logger { function log() { echo 'Log'; } } class User { use Logger; function log() { echo 'User Log'; } } $u = new User(); $u->log();
AFatal error: Method log() redeclared
BTrait cannot be used with class methods of same name
CSyntax error in trait declaration
DTrait method overridden by class method
Step-by-Step Solution
Solution:
  1. Step 1: Understand method precedence with traits

    If class defines method with same name as trait, class method overrides trait method.
  2. Step 2: Analyze output of $u->log()

    Since User defines log(), calling $u->log() runs User's method, printing 'User Log'.
  3. Final Answer:

    Trait method overridden by class method -> Option D
  4. Quick Check:

    Class method overrides trait method [OK]
Quick Trick: Class methods override trait methods with same name [OK]
Common Mistakes:
  • Expecting trait method to run instead
  • Thinking redeclaration causes error
  • Confusing trait with inheritance override

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes