<?php
/* sample code to show the implementation of adapter design pattern
* http://royads.blogspot.com
*/
// This is my base class library with 2 basic methods
class employee
{
private $first_name,$last_name;
function __construct($fname,$lname)
{
$this->first_name = $fname;
$this->last_name = $lname;
}
public function getFirstName()
{
return $this->first_name;
}
public function getLastName()
{
return $this->last_name;
}
}
// I need a single function now which will return full name.
// But I cant change this class.
// Solution is to implement the adapter class for employee and create new function in this adapter class.
// so lets do it.
class employeeAdapter
{
private $employee;
function __construct(employee $employee)
{
$this->employee = $employee;
}
public function getFullname()
{
return $this->employee->getFirstName()." - ".$this->employee->getLastName();
}
}
// Here I am just adapting the basic class methods in adapter class and not adding any new functionality in it. This is a way you can identify the adapter design pattern implementation.
$obj = new employee('firstname','lastname');
$objAdapter = new employeeAdapter($obj);
echo $objAdapter->getFullname();
?>