PHP › Interfaces & Abstract Classes

How do you decide whether to use an interface or an abstract class when designing a set of related classes that share common behavior?

Intermediate

Answer

You generally choose an interface when you want to define a pure contract without providing any shared implementation, especially when unrelated classes need to guarantee the same set of capabilities, and you choose an abstract class when you have genuinely shared implementation logic that multiple related subclasses can reuse as is, combined with certain behaviors that must still be customized by each specific subclass, keeping in mind that a class can implement multiple interfaces but can only extend a single abstract class.
// Interface: unrelated classes sharing a capability
interface Loggable { public function getLogMessage(): string; }

// Abstract class: related classes sharing implementation
abstract class PaymentMethod {
    public function logTransaction() { /* shared logic */ }
    abstract public function process(float $amount): bool;
}
Real-world example A payment processing system uses an abstract PaymentMethod class to share common transaction logging logic across all payment types, while separately using a Refundable interface that only some, but not all, payment methods choose to implement.

Common follow-ups: Why can a class only extend one abstract class but implement many interfaces?;What is a practical example where both an interface and an abstract class are used together in the same design?