Skip to content

Instantly share code, notes, and snippets.

@jmather jmather/example.php
Created Dec 16, 2012

Embed
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);
@cordoval

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.