PHP Interface example

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
}