Bird
0
0

Which is the best way to design this?

hard📝 Application Q15 of 15
PHP - Interfaces and Traits
You want to create a PHP class that must implement a method connect() defined by an interface, share common code for disconnect() from an abstract class, and reuse logging code from a trait. Which is the best way to design this?
ACreate an interface with connect() and disconnect(), a trait with logging, and a class that extends the interface and uses the trait.
BCreate an interface with connect(), an abstract class with disconnect(), a trait with logging, then a class that implements the interface, extends the abstract class, and uses the trait.
CCreate an abstract class with connect() and disconnect(), a trait with logging, and a class that implements the trait and extends the interface.
DCreate a trait with connect() and disconnect(), an abstract class with logging, and a class that extends the trait and implements the abstract class.
Step-by-Step Solution
Solution:
  1. Step 1: Understand roles of interface, abstract class, and trait

    Interface defines required methods like connect(). Abstract class can provide shared code like disconnect(). Trait is for reusable code like logging.
  2. Step 2: Combine them correctly in a class

    PHP allows a class to implement interfaces, extend one abstract class, and use multiple traits. So the class should implement the interface, extend the abstract class, and use the trait.
  3. Final Answer:

    Create an interface with connect(), an abstract class with disconnect(), a trait with logging, then a class that implements the interface, extends the abstract class, and uses the trait. -> Option B
  4. Quick Check:

    Interface + abstract class + trait combined properly [OK]
Quick Trick: Interface for methods, abstract class for code, trait for reuse [OK]
Common Mistakes:
  • Trying to extend multiple classes
  • Confusing trait and abstract class roles
  • Extending interface instead of implementing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes