When to implement and extend?

When to implement and extend?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

When to Implement and Extend?

Choosing between implement and extend is often the first question when designing a new class hierarchy, but the answer depends entirely on the relationship you are trying to define. They solve different problems: one enforces a contract without sharing state, while the other shares state and behavior.

The Contract vs. The Blueprint

Implementing an interface is a mechanism for enforcing a contract. When you use implements, you are declaring that your class promises to provide specific functionality defined by an external agreement. You do not inherit any methods or properties from that definition; you simply define them within your own scope.

In the e-commerce example you described, this is the correct approach. If you have a payment system where different providers (Stripe, PayPal, LocalBank) all need to support processPayment() and refundTransaction(), you should define an interface like PaymentProcessor. This ensures that before a new provider is integrated, it must strictly adhere to the required signature. The interface acts as a blueprint for behavior, not a source of code. It prevents you from accidentally calling a method that doesn't exist on your object, but it does not allow you to pass configuration or shared logic down through inheritance.

interface PaymentProcessor {
    public function processPayment(array $data): bool;
    public function refundTransaction(string $transactionId): void;
}

class StripeHandler implements PaymentProcessor {
    // We define our own storage, no inherited properties here.
    private string $apiKey;

    public function __construct(string $apiKey) {
        $this->apiKey = $apiKey;
    }

    public function processPayment(array $data): bool {
        // Implementation details specific to Stripe
        return true; 
    }
}

Inheritance for Shared State and Logic

Extending a class is reserved for scenarios where you need to share implementation details, common configuration, or default behavior. When you extend a class, the child inherits public and protected properties and methods from the parent. Private members remain encapsulated in the parent class.

Your session management example fits this pattern perfectly. A generic Session class likely handles core logic like hashing IDs, managing expiration times, or serializing data structures. Whether that data lives in a cookie or a database shouldn't change how the session is initialized or how it checks validity. By extending a base Session class, you avoid rewriting boilerplate code for every storage backend.

class Session {
    protected int $timeout = 1800;
    protected array $defaults = ['locale' => 'en'];

    public function __construct() {
        // Shared initialization logic
    }

    public function getTimeout(): int {
        return $this->timeout;
    }
}

class DatabaseSession extends Session {
    protected string $host = 'localhost';

    // We inherit $this->timeout automatically.
    // We only need to define database-specific logic.
}

Common Pitfalls and Trade-offs

The temptation to extend a class just to access its private variables is a frequent architectural mistake. While inheritance can seem like a shortcut for sharing configuration, it often leads to "fragile base classes." If you need to tweak a specific setting in the child class, doing so via inheritance couples that child tightly to the parent's internal state.

Conversely, using an interface when you actually need shared logic forces you to duplicate code or rely on dependency injection for every single property, which can become verbose quickly.

A robust design often combines both patterns. You might have a User class that implements Authenticatable (defining the contract of login/logout) while extending Loggable (inheriting logging logic). The key is to ask: "Am I defining what this object does, or am I inheriting how it works?"

If you are building a framework for others to use, interfaces are usually the safer bet because they allow users to inject their own implementations without being locked into your specific class structure. However, within your own internal library where you control the evolution of the codebase, strategic extension can significantly reduce duplication and keep configuration centralized.

Our network