Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/33/d337644085/htdocs/kieron-wp/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 77

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/33/d337644085/htdocs/kieron-wp/wp-content/plugins/jetpack/_inc/lib/class.media-summary.php on line 87

Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /homepages/33/d337644085/htdocs/kieron-wp/wp-content/themes/Divi/includes/builder/functions.php on line 4940
PHP Interface example - Kieron Howard
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: