PHP Professional এবং Zend Certified PHP Engineer (ZCPE ) Course এ সীমিত সংখ্যক আসন বাকি আছে। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করার জন্য অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল নম্বর : ০১৭২২ ৮১ ৭৫ ৯১

Adapter Design Pattern in PHP

Adapter Design Pattern in PHP

PHP adapter Design Pattern

The adapter pattern is a structural design pattern that allows you to re purpose a class with a different interface, allowing it to be used by a system which uses different calling methods.

Adapter: problem & solution



  • You want to reuse a library in your application but it doesn’t match your interface
  • You want to normalize multiple libraries to use the same interface
  • The Adapter pattern converts the interface of a class into another interface clients expect
  • Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces

Adapter: considerations

  • Adapter is a common pattern when using third party libraries
  • If you are developing your own libraries, you might want to avoid adapters and use common habits and guidelines
  • Often using in cooperation with other patterns, such as the Strategy pattern
  • Adapt or Modify?
  • Adapter pattern and Proxy pattern are similar, but they have different intents. Adapter is meant to change the interface of an existing object

Real Life Example:

class SimpleBook {
    private $author;
    private $title;
    function __construct($author_in, $title_in) {
        $this->author = $author_in;
        $this->title  = $title_in;
    function getAuthor() {
        return $this->author;
    function getTitle() {
        return $this->title;

class BookAdapter {
    private $book;
    function __construct(SimpleBook $book_in) {
        $this->book = $book_in;
    function getAuthorAndTitle() {
        return $this->book->getTitle().' by '.$this->book->getAuthor();

  // client


  $book = new SimpleBook("Gamma, Helm, Johnson, and Vlissides", "Design Patterns");
  $bookAdapter = new BookAdapter($book);
  writeln('Author and Title: '.$bookAdapter->getAuthorAndTitle());


  function writeln($line_in) {
    echo $line_in."<br/>";


Author and Title: Design Patterns by Gamma, Helm, Johnson, and Vlissides
In this example we have a SimpleBook class that has a getAuthor() and getTitle() methods. The client, expects a getAuthorAndTitle() method. To “adapt” SimpleBook for testAdapter we have an adapter class, BookAdapter, which takes in an instance of SimpleBook, and uses the SimpleBook getAuthor() and getTitle() methods in it’s own getAuthorAndTitle()method.
Adapters are helpful if you want to use a class that doesn’t have quite the exact methods you need, and you can’t change the orignal class. The adapter can take the methods you can access in the original class, and adapt them into the methods you need.
Adapter: using & abusing

  • Avoid creating adapter of adapters
  • Go to the main library and build the Adapter at the lowest possible level

Hi, My name is Masud Alam, love to work with Open Source Technologies, living in Dhaka, Bangladesh. I’m a Certified Engineer on ZEND PHP 5.3, I served my first five years a number of leadership positions at Winux Soft Ltd, SSL Wireless Ltd, Canadian International Development Agency (CIDA), World Vision, Care Bangladesh, Helen Keller, US AID and MAX Group where I worked on ERP software and web development., but now i’m a founder and CEO of TechBeeo Software Company Ltd. I’m also a Course Instructor of ZCPE PHP 7 Certification and professional web development course at w3programmers Training Institute – a leading Training Institute in the country.

One comment on “Adapter Design Pattern in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

No announcement available or all announcement expired.