Answer the question
In order to leave comments, you need to log in
How to change type of method parameter in php on override?
Let's say we have two interfaces
interface Message {
function send();
function setSender(Sender $sender);
}
interface Sender {
function send(Message $message);
}
class EmailMessage implements Message {
private $sender;
public function setSender(Sender $sender) {
$this->sender = $sender;
}
function send() {
$this->sender->send($this);
}
}
class SmtpEmailMessageSender implements Sender{
private $server;
private $login;
private $password;
public function __construct($server, $login, $password) {
$this->server = $server;
$this->login = $login;
$this->password = $password;
}
function send(Message $message) {
..... код отправки.....
}
}
Answer the question
In order to leave comments, you need to log in
class SmtpEmailMessageSender implements Sender
{
private $server;
private $login;
private $password;
public function __construct($server, $login, $password)
{
$this->server = $server;
$this->login = $login;
$this->password = $password;
}
function send(Message $message)
{
if( !($message instanceof EmailMessage) ){throw new InvalidArgumentException("Instance of 'EmailMessage' expected. Got '".get_class($message)."'");}
//...
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question