Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Proxy Pattern Example
<?php
interface MyAPI
{
public function getThing();
}
class MyApiImplentation implements MyAPI
{
public function getThing()
{
return 'some remote call';
}
}
class MyApiCache implements MyAPI
{
private $cache;
private $api;
public function __construct($cache, MyAPI $api)
{
$this->cache = $cache;
$this->api = $api;
}
public function getThing()
{
if ($cache->hasItem() == false)
{
$info = $this->api->getThing();
$this->cache->store($info);
}
return $this->cache->getItem();
}
}
$api = new MyAPIImplementation();
$cache = new SomeCacheSystem();
$real_api_to_use = new MyApiCache($cache, $api);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment