Bird
0
0

What is a key difference between an abstract class and a trait in PHP?

easy📝 Conceptual Q2 of 15
PHP - Interfaces and Traits
What is a key difference between an abstract class and a trait in PHP?
AAn abstract class can be instantiated, but a trait cannot.
BAn abstract class cannot have method implementations, but a trait can.
CA trait cannot have properties, but an abstract class can.
DA trait is used to reuse code in multiple classes, an abstract class is a base class.
Step-by-Step Solution
Solution:
  1. Step 1: Understand traits and abstract classes

    Traits are for code reuse across classes; abstract classes serve as base classes with some implemented methods.
  2. Step 2: Analyze the options

    Abstract classes can have properties and method implementations and cannot be instantiated directly. Traits can have properties and methods but are not classes.
  3. Final Answer:

    A trait is used to reuse code in multiple classes, an abstract class is a base class. -> Option D
  4. Quick Check:

    Trait = code reuse, Abstract class = base class [OK]
Quick Trick: Traits reuse code; abstract classes define base behavior [OK]
Common Mistakes:
  • Believing abstract classes can be instantiated
  • Assuming traits cannot have properties
  • Confusing traits with interfaces

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes