Created
July 27, 2017 11:48
-
-
Save Clivern/9717362ae3f25882d9fa73daaecbf251 to your computer and use it in GitHub Desktop.
OOP Class Implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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