As everybody knows, the point of wrapper classes is encapsulates the functionality of another class or component . Here is a simple Class that wrap a small part of PHP Predis library :
class CacheWrapper {
private $client;
public function __construct(){
$this->client = new Predis\Client();
}
public function set($key, $value){
$this->client->set($key, $value);
}
public function get($key){
return $this->client->get($key);
}
}
and here is the simple code that use this wrapper class :
$client = new CacheWrapper();
echo $client->get('key1');
Where this class would work perfectly the problem that it create the dependency inside the class which i want to avoid by injecting the dependency into the class instead of letting the class to create its dependency , and so the wrapper class will look like this :
class CacheWrapper {
private $client;
public function __construct(Predis\Client $predisObj){
$this->client = $predisObj;
}
public function set($key, $value){
$this->client->set($key, $value);
}
public function get($key){
return $this->client->get($key);
}
}
so i have to write the following code to use the wrapper class :
$predis = new Predis\Client();
$client = new CacheWrapper($predis);
echo $client->get('key1');
But i think like that there is no point to use the wrapper class since i still use the original class in my code . so my question is: does dependency injection and wrapper class goes against each other and can't be used together and what is the best way to solve issue like this ?
Aucun commentaire:
Enregistrer un commentaire