DEV Community

Cover image for PHP Design Pattern: Adapter
Antonio Silva
Antonio Silva

Posted on

PHP Design Pattern: Adapter

The Adapter Design Pattern is a structural pattern that allows objects with incompatible interfaces to work together. It acts as an intermediary (or adapter) between two objects, converting the interface of one object to the interface expected by the other. This allows classes that would otherwise be incompatible because they have different interfaces to cooperate without modifications to their original code.

Adapter Structure

The Adapter pattern is generally composed of three main elements:

  • Client: The class that expects to work with objects of a specific interface.
  • Adaptee: The class that has an interface that is incompatible with the client, but whose functionalities are necessary.
  • Adapter: The class that implements the interface expected by the client and converts calls to the Adaptee interface.

UMl

Types of Adapters

  1. Object Adapter: Composition-based. The Adapter contains an instance of the class it is adapting.
  2. Class Adapter: Inheritance-based (usually in languages ​​that support multiple inheritance).

When to use the Adapter?

  • When you want to use an existing class, but its interface does not match what the client expects.
  • To integrate new functionality into a legacy system, without having to modify the old code.

This pattern is useful in systems that need to work with external libraries or APIs, allowing you to adapt their functionality without changing the code of these libraries.

Example Using PHPMailer

Here is an example of how to use the Adapter Design Pattern to integrate PHPMailer with a custom interface.

Situation:

Let's assume that your system expects any email sending class to implement an interface called IMailer, but PHPMailer does not follow this interface directly. The Adapter will be used to adapt PHPMailer to the interface expected by the system.

Install PHPMailer via Composer

composer require phpmailer/phpmailer
Enter fullscreen mode Exit fullscreen mode

Directory System

📦Adapter
 ┣ 📂src
 ┃ ┣ 📂Interfaces
 ┃ ┃ ┗ 📜IMailer.php
 ┃ ┣ 📂Adapters
 ┃ ┃ ┗ 📜PHPMailerAdapter.php
 ┃ ┗ 📂Services
 ┃   ┗ 📜ServicoDeEmail.php
 ┣ 📂vendor
 ┣ 📜composer.json
 ┗ 📜index.php
Enter fullscreen mode Exit fullscreen mode

Autoload

In the composer.json file (located at the root of the project), add the App namespace to automatically load the classes:

{
    "autoload": {
        "psr-4": {
            "App\\": "src/"
        }
    },
    "require": {
        "phpmailer/phpmailer": "^6.5"
    }
}
Enter fullscreen mode Exit fullscreen mode

Interface IMailer

namespace App\Interfaces;

interface IMailer {
    public function send($to, $subject, $message);
}
Enter fullscreen mode Exit fullscreen mode

Class PHPMailerAdapter

namespace App\Adapters;

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
use App\Interfaces\IMailer;

class PHPMailerAdapter implements IMailer {
    private $phpMailer;

    public function __construct() {
        $this->phpMailer = new PHPMailer(true);

        // Basic PHPMailer configuration
        $this->phpMailer->isSMTP();
        $this->phpMailer->Host = 'smtp.example.com';
        $this->phpMailer->SMTPAuth = true;
        $this->phpMailer->Username = 'your-email@example.com';
        $this->phpMailer->Password = 'password';
        $this->phpMailer->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
        $this->phpMailer->Port = 587;
        $this->phpMailer->setFrom('your-email@example.com', 'Your Name');
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Method send
public function send($to, $subject, $message) {
    try {
        $this->phpMailer->addAddress($to);
        $this->phpMailer->Subject = $subject;
        $this->phpMailer->Body = $message;
        $this->phpMailer->send();
        echo 'Email sent successfully!';
    } catch (Exception $e) {
        echo "Failed to send email: {$this->phpMailer->ErrorInfo}";
    }
}
Enter fullscreen mode Exit fullscreen mode

Class EmailService

namespace App\Services;

use App\Interfaces\IMailer;

class EmailService {
    private $mailer;

    public function __construct(IMailer $mailer) {
        $this->mailer = $mailer;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Method sendEmailToClient
public function sendEmailToClient($to, $subject, $message) {
    $this->mailer->send($to, $subject, $message);
}
Enter fullscreen mode Exit fullscreen mode

File index.php

require 'vendor/autoload.php';

use App\Adapters\PHPMailerAdapter;
use App\Services\EmailService;

// Creating the PHPMailer Adapter
$mailer = new PHPMailerAdapter();

// Using the email service with the IMailer interface
$emailService = new EmailService($mailer);

// Sending an email
$emailService->sendEmailToClient('client@example.com', 'Email Subject', 'Message content.');
Enter fullscreen mode Exit fullscreen mode

Explanation of the Structure

  • IMailer.php: Defines the IMailer interface that any email system should implement.
  • PHPMailerAdapter.php: Adapts PHPMailer to the IMailer interface.
  • EmailService.php: Email service that uses the IMailer interface to send emails.
  • index.php: Main file that uses the email service to send a message.

Top comments (2)

Collapse
 
richmirks profile image
Richard Mirks

Interesting post! Could you explain the key differences between an Object Adapter and a Class Adapter in a bit more detail? I'd love to know when you'd choose one over the other.

Collapse
 
xxzeroxx profile image
Antonio Silva

The difference between Class Adapter and Object Adapter is in the way adaptation is implemented to allow communication between two incompatible interfaces. Both types follow the same general principle, but differ in their structure and usage.

Class Adapter

The Class Adapter is implemented using inheritance. It adapts an incompatible interface to another by inheriting from both classes (or a class and an interface) and redefining or modifying the necessary methods. This is only possible in languages ​​that support multiple inheritance.

Features:

  • Uses inheritance to adapt the interface.
  • Only works in languages ​​with multiple inheritance.
  • The Class Adapter is usually more tightly coupled to the adapted class.

Class Adapter Example

Imagine we have a class OldLogger with a logMessage() method, but we want to adapt it to a LoggerInterface interface that requires the log() method.

In a language with multiple inheritance, the Class Adapter could be implemented like this:

// Interface we want the Adapter to implement
interface LoggerInterface {
    void log(String message);
}

// Existing class that we want to adapt
class OldLogger {
    public void logMessage(String message) {
        System.out.println("Logging: " + message);
    }
}

// Class Adapter (Java does not support multiple inheritance, but in C++ this would be feasible)
class LoggerAdapter extends OldLogger implements LoggerInterface {
    public void log(String message) {
        // Adapts the call to the existing class method
        logMessage(message);
    }
}

Enter fullscreen mode Exit fullscreen mode

Here, LoggerAdapter inherits from OldLogger and implements LoggerInterface, adapting the log() method to call logMessage().

Object Adapter

The Object Adapter is implemented using composition instead of inheritance. Instead of inheriting from the class that needs to be adapted, it contains an instance of that class and delegates method calls to it. This provides more flexibility and is a more common solution because it avoids the problems of multiple inheritance.

Features:

  • Uses composition to adapt the interface.
  • Works in languages ​​without multiple inheritance.
  • Generally more flexible and decoupled.

Object Adapter Example

Let's adapt the same OldLogger to LoggerInterface, but now using the Object Adapter:

// Interface that we want the adapter to implement
interface LoggerInterface {
    public function log($message);
}

// Existing class we want to adapt
class OldLogger {
    public function logMessage($message) {
        echo "Logging: " . $message;
    }
}

// Object Adapter
class LoggerAdapter implements LoggerInterface {
    private $oldLogger;

    public function __construct(OldLogger $oldLogger) {
        $this->oldLogger = $oldLogger;
    }

    public function log($message) {
        // Adapts the call to the method of the existing class
        $this->oldLogger->logMessage($message);
    }
}

Enter fullscreen mode Exit fullscreen mode
$oldLogger = new OldLogger();
$loggerAdapter = new LoggerAdapter($oldLogger);
$loggerAdapter->log("This is a message."); // Outputs: Logging: This is a message.

Enter fullscreen mode Exit fullscreen mode

Here, LoggerAdapter does not inherit from OldLogger; instead, it holds an instance of OldLogger and calls logMessage() through it. This allows you to swap out the internal OldLogger instance easily if needed.

In PHP, it is not possible to implement a Class Adapter , as PHP does not support multiple inheritance.

Summary

Feature Class Adapter Object Adapter
Implementation Uses inheritance Uses composition
Multiple Inheritance Requires multiple inheritance support Works in any language
Flexibility Less flexible, tightly coupled More flexible, loosely coupled
Commonality Less common More common

In general, Object Adapters are preferred because they offer greater flexibility and avoid the pitfalls of multiple inheritance.