Select Page


I’ve never really come across the need to use an interface, and usually just use inheritance to extend whatever class I needed to. Recently though I needed a generic base caching class, which would have other classes implement there own methods.


interface CacheInterface

{
public function getCacheValue($key);

public function setCacheValue($key , $value);
}

Note that there is no content within the methods, and rather than curly braces, its closed off with a ‘;’.

To implement the interface:


class CacheGoogle extends Controller implements CacheInterface
{

public function getCacheValue($key)
{
//try and get cached value from the DB
}

public function setCacheValue($key , $value)
{
//add new cache value to DB
}
}

Then wherever its needed:


private $cache;

public function __construct()
{
$this->cache = new CacheGoogle;
}

public function checkCache(CacheInterface $this->cache)
{
//
}

%d bloggers like this: