Tag Archives: Adapter

Adapter Design Pattern in PHP5

Written by Garry Lachman (Admin). Filed under PHP + mySQL. Tagged , , , . .

Adapter patter is very simply pattern that make an interface
for another class using his functions

ExampleItem.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
class ExampleItem   {
    private $name;
    private $title;

    private fucntion __construct($name, $title) {
        $this->name = $name;
        $this->title = $title;
    }

    public function getName()   {
        return $this->name;
    }

    public function getTitle()  {
        return $this->title;
    }
}
?>

ExampleItemAdapter.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
include("ExampleItem.php");

class ExampleItemAdapter    {
    private $exampleItem;

    private fucntion __construct(ExampleItem $exampleItem)  {
        $this->exampleItem = $exampleItem;
    }

    public function getItemInfo()   {
        return $this->exampleItem->getName() . " - " . $this->exampleItem->getTitle();
    }
}
?>

testAdapter.php

1
2
3
4
5
6
7
8
9
<?php
include("ExampleItem.php");
include("ExampleItemAdapter.php");

$test = new ExampleItemAdapter(new ExampleItem("Garry Lachman","SomeTitle"));

echo $test->getItemInfo();
// result: "Garry Lachman - SomeTitle"
?>

Have Fun ;)
Garry Lachman

Share