


Detailed description of PHP adapter pattern (with code examples)
This article brings you relevant knowledge about PHP. It mainly talks about the PHP adapter mode and code examples. Friends who are interested can take a look below. I hope it will be helpful to you.
PHP Adapter Pattern Explanation and Code Examples
Adapter is a structural design Pattern, which enables incompatible objects to cooperate with each other.
The adapter can serve as a wrapper between two objects. It will receive a call to one object and convert it into a format and interface recognized by the other object.
Complexity: ******
Popularity: ******
Usage examples: The adapter pattern is very common in PHP code. Systems based on some legacy code often use this pattern. In this case, adapters allow legacy code to work with modern classes.
Identification method: Adapters can be identified through constructors that take instances of different abstract or interface types as parameters. When any method of the adapter is called, it converts the parameters into the appropriate format and then directs the call to one or more methods in its encapsulated object.
Real World Example
Adapters allow you to use classes from third-party or legacy systems even if they are incompatible with your code. For example, you can create a series of special wrappers to adapt the calls made by the application to the interface and format required by the third-party class, without having to rewrite the application's notification interface to support each third-party service ( Such as DingTalk, WeChat, SMS or any other service).
index.php: Real world example
<?php namespace RefactoringGuru\Adapter\RealWorld; /** * The Target interface represents the interface that your application's classes * already follow. */ interface Notification { public function send(string $title, string $message); } /** * Here's an example of the existing class that follows the Target interface. * * The truth is that many real apps may not have this interface clearly defined. * If you're in that boat, your best bet would be to extend the Adapter from one * of your application's existing classes. If that's awkward (for instance, * SlackNotification doesn't feel like a subclass of EmailNotification), then * extracting an interface should be your first step. */ class EmailNotification implements Notification { private $adminEmail; public function __construct(string $adminEmail) { $this->adminEmail = $adminEmail; } public function send(string $title, string $message): void { mail($this->adminEmail, $title, $message); echo "Sent email with title '$title' to '{$this->adminEmail}' that says '$message'."; } } /** * The Adaptee is some useful class, incompatible with the Target interface. You * can't just go in and change the code of the class to follow the Target * interface, since the code might be provided by a 3rd-party library. */ class SlackApi { private $login; private $apiKey; public function __construct(string $login, string $apiKey) { $this->login = $login; $this->apiKey = $apiKey; } public function logIn(): void { // Send authentication request to Slack web service. echo "Logged in to a slack account '{$this->login}'.\n"; } public function sendMessage(string $chatId, string $message): void { // Send message post request to Slack web service. echo "Posted following message into the '$chatId' chat: '$message'.\n"; } } /** * The Adapter is a class that links the Target interface and the Adaptee class. * In this case, it allows the application to send notifications using Slack * API. */ class SlackNotification implements Notification { private $slack; private $chatId; public function __construct(SlackApi $slack, string $chatId) { $this->slack = $slack; $this->chatId = $chatId; } /** * An Adapter is not only capable of adapting interfaces, but it can also * convert incoming data to the format required by the Adaptee. */ public function send(string $title, string $message): void { $slackMessage = "#" . $title . "# " . strip_tags($message); $this->slack->logIn(); $this->slack->sendMessage($this->chatId, $slackMessage); } } /** * The client code can work with any class that follows the Target interface. */ function clientCode(Notification $notification) { // ... echo $notification->send("Website is down!", "<strong style='color:red;font-size: 50px;'>Alert!</strong> " . "Our website is not responding. Call admins and bring it up!"); // ... } echo "Client code is designed correctly and works with email notifications:\n"; $notification = new EmailNotification("developers@example.com"); clientCode($notification); echo "\n\n"; echo "The same client code can work with other classes via adapter:\n"; $slackApi = new SlackApi("example.com", "XXXXXXXX"); $notification = new SlackNotification($slackApi, "Example.com Developers"); clientCode($notification);
Output.txt: Execution result
Client code is designed correctly and works with email notifications: Sent email with title 'Website is down!' to 'developers@example.com' that says '<strong style='color:red;font-size: 50px;'>Alert!</strong> Our website is not responding. Call admins and bring it up!'. The same client code can work with other classes via adapter: Logged in to a slack account 'example.com'. Posted following message into the 'Example.com Developers' chat: '#Website is down!# Alert! Our website is not responding. Call admins and bring it up!'.
Conceptual example
This example illustrates the structure of the Adapter design pattern and focuses on answering the following questions:
- What classes does it consist of?
- What roles do these classes play?
- How do the elements in the pattern relate to each other?
After understanding the structure of this pattern, you can more easily understand the following real-world PHP application cases.
index.php: Concept example
<?php namespace RefactoringGuru\Adapter\Conceptual; /** * The Target defines the domain-specific interface used by the client code. */ class Target { public function request(): string { return "Target: The default target's behavior."; } } /** * The Adaptee contains some useful behavior, but its interface is incompatible * with the existing client code. The Adaptee needs some adaptation before the * client code can use it. */ class Adaptee { public function specificRequest(): string { return ".eetpadA eht fo roivaheb laicepS"; } } /** * The Adapter makes the Adaptee's interface compatible with the Target's * interface. */ class Adapter extends Target { private $adaptee; public function __construct(Adaptee $adaptee) { $this->adaptee = $adaptee; } public function request(): string { return "Adapter: (TRANSLATED) " . strrev($this->adaptee->specificRequest()); } } /** * The client code supports all classes that follow the Target interface. */ function clientCode(Target $target) { echo $target->request(); } echo "Client: I can work just fine with the Target objects:\n"; $target = new Target(); clientCode($target); echo "\n\n"; $adaptee = new Adaptee(); echo "Client: The Adaptee class has a weird interface. See, I don't understand it:\n"; echo "Adaptee: " . $adaptee->specificRequest(); echo "\n\n"; echo "Client: But I can work with it via the Adapter:\n"; $adapter = new Adapter($adaptee); clientCode($adapter);
Output.txt: Execution result
Client: I can work just fine with the Target objects: Target: The default target's behavior. Client: The Adaptee class has a weird interface. See, I don't understand it: Adaptee: .eetpadA eht fo roivaheb laicepS Client: But I can work with it via the Adapter: Adapter: (TRANSLATED) Special behavior of the Adaptee.
Recommended study:《PHP video tutorial》
The above is the detailed content of Detailed description of PHP adapter pattern (with code examples). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c
