Bird
0
0

Find the problem in this PHP code:

medium📝 Debug Q7 of 15
PHP - Interfaces and Traits
Find the problem in this PHP code:
trait T1 { public function hello() { return "Hello"; } } trait T2 { public function hello() { return "Hi"; } } class D { use T1, T2; } $obj = new D(); echo $obj->hello();
AFatal error due to method name conflict
BOutputs "Hello"
COutputs "Hi"
DOutputs nothing
Step-by-Step Solution
Solution:
  1. Step 1: Understand trait conflict rules

    When multiple traits used in a class have methods with the same name, PHP throws a fatal error unless conflict is resolved.
  2. Step 2: Analyze the code

    Traits T1 and T2 both define hello(), class D uses both without conflict resolution, causing fatal error.
  3. Final Answer:

    Fatal error due to method name conflict -> Option A
  4. Quick Check:

    Trait method conflict = Fatal error [OK]
Quick Trick: Resolve trait method conflicts to avoid fatal errors [OK]
Common Mistakes:
  • Ignoring method name conflicts in multiple traits
  • Expecting last trait to override silently
  • Not using insteadof or as to resolve conflicts

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes