Bird
0
0

What will be the output of this PHP code?

medium📝 Predict Output Q5 of 15
PHP - Interfaces and Traits
What will be the output of this PHP code?
trait A { function say() { echo 'A'; } } trait B { function say() { echo 'B'; } } class Test { use A, B { B::say insteadof A; } } $t = new Test(); $t->say();
AA
BB
CAB
DError: Trait conflict
Step-by-Step Solution
Solution:
  1. Step 1: Identify trait conflict resolution

    Both traits A and B define say(), so conflict arises.
  2. Step 2: Analyze conflict resolution syntax

    Using B::say insteadof A; means B's say() is used, ignoring A's.
  3. Final Answer:

    B -> Option B
  4. Quick Check:

    Trait conflict resolved = B method used [OK]
Quick Trick: Use insteadof to resolve trait method conflicts [OK]
Common Mistakes:
  • Expecting both methods to run
  • Ignoring conflict resolution syntax
  • Assuming error without resolution

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes