Skip to content

Instantly share code, notes, and snippets.

@Clivern
Created July 27, 2017 11:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Clivern/9717362ae3f25882d9fa73daaecbf251 to your computer and use it in GitHub Desktop.
Save Clivern/9717362ae3f25882d9fa73daaecbf251 to your computer and use it in GitHub Desktop.
OOP Class Implementation
<?php
interface Storage
{
public function set($data);
public function get();
public function store();
}
class MySQL implements Storage
{
private $data;
public function set($data){
echo "MySQL Set";
$this->data = $data;
}
public function get(){
echo "MySQL Get";
return $this->data;
}
public function store(){
echo "MySQL Store";
}
}
class ElasticSearch implements Storage
{
private $data;
public function set($data){
echo "ElasticSearch Set";
$this->data = $data;
}
public function get(){
echo "ElasticSearch Get";
return $this->data;
}
public function store(){
echo "ElasticSearch Store";
}
}
class Redis implements Storage
{
private $data;
public function set($data){
echo "Redis Set";
$this->data = $data;
}
public function get(){
echo "Redis Get";
return $this->data;
}
public function store(){
echo "Redis Store";
}
}
class Base
{
private $storage_handler;
public function __construct(Storage $storage_handler)
{
$this->storage_handler = $storage_handler;
}
public function test()
{
$this->storage_handler->set("Hello World");
echo "<br/>";
$this->storage_handler->get();
echo "<br/>";
$this->storage_handler->store();
}
}
$base = new Base(new MySQL());
$base->test();
echo "<br/><br/>";
$base = new Base(new ElasticSearch());
$base->test();
echo "<br/><br/>";
$base = new Base(new Redis());
$base->test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment