Bird
0
0

Why are traits needed in PHP?

easy📝 Conceptual Q11 of 15
PHP - Interfaces and Traits
Why are traits needed in PHP?
trait Logger { function log($msg) { echo $msg; } }
ATo reuse methods across multiple classes without inheritance
BTo create new classes from scratch
CTo replace interfaces completely
DTo define constants only
Step-by-Step Solution
Solution:
  1. Step 1: Understand traits purpose

    Traits allow sharing methods among classes without using inheritance.
  2. Step 2: Compare with other options

    Traits are not for creating classes, replacing interfaces, or defining constants only.
  3. Final Answer:

    To reuse methods across multiple classes without inheritance -> Option A
  4. Quick Check:

    Traits = Code reuse without inheritance [OK]
Quick Trick: Traits share code without inheritance, unlike classes [OK]
Common Mistakes:
  • Thinking traits create new classes
  • Confusing traits with interfaces
  • Believing traits only hold constants

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes