Adapter pattern PHP

When you’re programming on a large code base, which is maintained by several developers then you will have come across this issue; a team member updates their method signature, or a new requirement is requested which has not been  provided previously.  If the method signature has been changed, and your code relies on the method, then you will be tempted to update the signatures. However, this is not the ideal solution as the method signature might change anytime. This is why you would need to implement the Adapter pattern.

The Adapter pattern helps us to cope with new or additional changes.

For example your team member has written a class ContactReader and ContactSaver

The functionality is simple. ContactReader reads data from an API and ContactSaver saves the data into the database.

 

class ContactReader = reads the contact from api
class ContactSaver = saves the contacts into database

class ContactReader 
{
    /**
      @var array Contacts
    */
    public $contacts;

    /**
    Loads contacts
    **/
    public function contacts()
    {
 
       $this->contacts = // fetch contact from API
       return $this;
          
    }
}

 

 

class ContactSaver
{

    /**
     Saves contact
     @param  Contacts
    **/
    public function saveContact($contact)
    {
              //saves the contact into database
    }    
}

This is how the classes are being used.


$objContactReader = new ContactReader();
$contacts = $objContactReader->contacts();

$objContactSaver = new ContactSaver();
$objContactSaver->saveContact($contacts);

From the above, we can conclude that class ContactReader returns an instance of itself with the contacts, ContactSaver method saveContact accepts the contacts and saves it into the database.

However, a new requirement is requested to modify the data prior to saving it to the database.
Such requirement is not provided by your team member class. We will use the adopter pattern to provide this new feature and at the same time maintain the team members class functionality.

Our Adapter class will extend ContactSave, which inherits its functionality and modify the contacts providing the new feature.

ContactSaverAdapter


class ContactSaverAdapter extends ContactSaver

{

   /**
    Save contacts
    @var Contacts
   **/
   public function saveContact($contacts)

   {

      // our code to modify the data 
     // 
      parent::saveContact($contacts);

   }
}

 

Our main class process will change to:

nbsp;


$objContactReader = new ContactReader();
$contacts = $objContactReader->contacts();

// we create an instance of adapter this time
$objContactSaver = new ContactSaverAdapter();
$objContactSaver->saveContact($contacts);

In the Adapter pattern we should always ensure that all of the operations of ContactSaver remains, Adapter pattern provides extra functionality over the old class.

The Adapter pattern might seem time consuming but in the long run it actually saves time.

It is best practice to write an Adapter for any api you use, as it might change anytime.