ADAPTER PATTERN -PHP

Uğur Müslim
3 min readDec 12, 2020

You can find the patterns I write in other languages but I am writing the language on the end of the title to inform you which language will be there for examples.

You use every pattern I wrote in your frameworks without knowing it. After learning these concepts, overall knowledge of these framework architectures will be better. Today I’ll write about Adapter Pattern.

The name will give you a hint. Adapters are like converters. You use plug adapters in UK right if you are traveling from another country right?

Let’s go with an example;

You have a chef and an owen. All we gonna do is turning on that owen, we will not go further.

interface OwenInterface
{
public function turnOn();

public function checkLeakage();

}

class Owen implements OwenInterface
{
public function turnOn()
{
var_dump('Turn the gas on');
}

public function checkLeakage()
{
var_dump('Check if there is gas leakage');
}
}

We created an interface and a class. In this class we are turning the owen on and checking for gas leakage. 2 days later you heard that a new electric owen will be used. So will you change functions and names. Or will you put two or more extra functions to Owen class. No, forget them there is a better way.

interface ElectricOwenInterface
{
public function pressOnButton();

public function checkResistance();

}

class ElectricOwen implements ElectricOwenInterface
{
public function pressButton()
{
var_dump('Press on button');
}

public function checkResistance()
{
var_dump('Check the electrical resistance');
}
}

We created an ElectricOwen class also. We have got two seperate classes and different functions. How can we bind them?

class ElectricOwenAdapter implements OwenInterface
{

/**
*
@var ElectricOwenInterface
*/
private $electricOwen;

public function __construct(ElectricOwenInterface $electricOwen)
{
$this->electricOwen = $electricOwen;
}

public function turnOn()
{
$this->electricOwen->pressOnButton();
}

public function checkLeakage()
{
$this->electricOwen->checkResistance();
}
}

As you see this adapter class implements our old and beloved OwenInterface, therefore uses the same functions but in these functions it is integrating our ElectricOwen class’ functions. So we can do something like this;

class Chef {
public function cook(OwenInterface $owen){
$owen->turnOn();
$owen->checkLeakage();
}
}

(new Chef)->cook(new ElectricOwenAdapter(new ElectricOwen));

And what will we have?

string(15) “Press on button”
string(31) “Check the electrical resistance”

But if we use it without adapter we ill just use our old owen class;

(new Chef)->cook(new Owen());string(15) "Turn the gas on"
string(29) "Check if there is gas leakage"

As you see Adapter Pattern is pretty useful. Without changing function names and the grand logic you can integrate new classes to your system. All the patterns are trying to solve a problem and ease developer’s pain but adapter pattern make it in a really smooth way I think.

As always I‘ll put a link if you want to watch a video.

--

--

Uğur Müslim

Software developer, guitar player, drinks mostly tea and water. Lately reads history books. Trying to get away from fiction.