Bird
0
0

You want to share logging functionality across unrelated classes User and Product. How can traits help?

hard📝 Application Q8 of 15
PHP - Interfaces and Traits
You want to share logging functionality across unrelated classes User and Product. How can traits help?
ADefine a Logger trait with log() method and use it in both classes
BMake User and Product extend Logger class
CImplement Logger interface in both classes
DCopy log() method code into both classes
Step-by-Step Solution
Solution:
  1. Step 1: Identify code reuse need

    Both User and Product need logging, but they are unrelated classes.
  2. Step 2: Use trait for shared methods

    Defining a Logger trait with log() method and including it in both classes allows reuse without inheritance.
  3. Final Answer:

    Define a Logger trait with log() method and use it in both classes -> Option A
  4. Quick Check:

    Traits enable shared methods in unrelated classes [OK]
Quick Trick: Use traits to share methods across unrelated classes [OK]
Common Mistakes:
  • Trying to extend multiple classes
  • Using interfaces without method code
  • Copy-pasting code instead of reusing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes