Bird
0
0

You want to share a method implementation across multiple unrelated classes without using inheritance. Which PHP feature should you use?

hard📝 Application Q8 of 15
PHP - Interfaces and Traits
You want to share a method implementation across multiple unrelated classes without using inheritance. Which PHP feature should you use?
AAbstract class
BStatic class
CInterface
DTrait
Step-by-Step Solution
Solution:
  1. Step 1: Understand the problem context

    Sharing method code across unrelated classes means no inheritance is possible.
  2. Step 2: Identify PHP feature for code reuse without inheritance

    Traits allow code reuse by injecting methods into multiple classes regardless of inheritance.
  3. Final Answer:

    Trait -> Option D
  4. Quick Check:

    Code reuse without inheritance = Trait [OK]
Quick Trick: Use traits to share code across unrelated classes [OK]
Common Mistakes:
  • Trying to use abstract classes for unrelated classes
  • Using interfaces which only declare methods
  • Confusing static classes with traits

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes