Answer the question
In order to leave comments, you need to log in
How to put similar entities into one abstract class?
There is a service for sending SMS messages. When sending messages through its API, the provider parameter is indicated (from which provider the messages will be sent), as you understand, each provider has its own API, the corresponding methods are named differently for everyone (for example, somewhere / send / sms ,, somewhere /sendsms or /send, etc., /balance or /getbalance, etc.)
For each provider, there is a separate class with its methods, so the question is how to do something like a single class with methods (sendSms (), getBalance(), etc.) through which the provider class will be selected. I hope I wrote clearly :)
Answer the question
In order to leave comments, you need to log in
Quick option
class Sms
{
protected static $instances = [];
public static function getInstance($id = 'default')
{
$map = [
'first' => 'FirstSmsHandler',
'second' => 'SecondSmsHandler',
'default' => 'FirstSmsHandler',
];
if (!isset(self::$instances[$id])) {
if (!isset($map[$id])) {
throw new \Exception(sprintf('Unknown hadler `%s`', $id));
}
$handler = new $map[$id];
self::$instances[$id] = new self($handler);
}
return self::$instances[$id];
}
protected $handler;
public function __construct(SmsHandler $handler)
{
$this->setHandler($handler)
}
public function setHandler(SmsHandler $handler)
{
return $this->handler = $handler;
}
public function changeHandlerByName($handlerName)
{
$map = [
'first' => 'FirstSmsHandler',
'second' => 'SecondSmsHandler',
'default' => 'FirstSmsHandler',
];
if (!isset($map[$handlerName])) {
throw new \Exception(sprintf('Unknown handler `%s`', $handlerName));
}
$handler = new $map[$handlerName];
$this->setHandler($handler);
return $this;
}
public function __call($name, $arguments)
{
return call_user_func_array([$this->handler, $name], $arguments);
}
}
$sms = new Sms(new FirstSmsHandler());
$sms->changeHandlerByName($_POST['provider']);
$sms = Sms::getInstance($_POST['provider']);
$sms->getBalance();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question