laserlight wrote:Yes. Just define a setUserData() member function in RegisterMail that matches the parameter list of the setUserData() member function in UserMail. At the moment you are overloading setUserData() instead of overriding
.
Thanks for the explanation.
but function overloading is not allowed in PHP.
http://it2.php.net/manual/it/language.oop5.overloading.php#77843
😕
Of course, you may say that while all UserMail requires a $to and $uid, a RegisterMail also requires a $username and $password. In that case, you may want to define a parallel UserData hierarchy, and have the RegisterMail constructor take RegisterData object
Thanks for the tips.
In short I've done this implementation but it doesn't work
and I don't understand why :
<?php
class UserData{
public function __construct() {}
}
class ResetUserData extends UserData{
public $to= '';
public $uid= '';
public function __construct($to,$uid){
$this->to= $to;
$this->uid= $uid;
}
}
class RegisterUserData extends UserData{
public $to= '';
public $uid= '';
public $username= '';
public $password= '';
public function __construct($to,$uid,$username,$password='') {
$this->to= $to;
$this->uid= $uid;
$this->username= $username;
$this->password= $password;
}
}
abstract class UserMail{
protected $to= '';
protected $subject= '';
protected $body= '';
public function __construct() {}
public function send(){
$mail = new Mail($this->to, $this->subject, '', FROM_MAIL);
$mail->setBodyHtml($this->body);
$mail->setPriority(AbstractMail::NORMAL_PRIORITY);
if(!$mail->send()){
throw new MailException('The mail can\'t be sent in class <b>['.__CLASS__.']</b>');
}
}
abstract protected function setBody();
abstract protected function setSubject();
abstract protected function setUserData(UserData $data);
}
class ResetMail extends UserMail{
public function __construct() {
parent::__construct();
}
public function setUserData(ResetUserData $data){
$this->to= $data->to;
$this->uid= $data->uid;
$this->setSubject();
$this->setBody();
}
protected function setBody(){}
protected function setSubject(){}
}
class RegisterMail extends UserMail{
private $username= '';
private $password= '';
public function __construct() {
parent::__construct();
}
public function setUserData(RegisterUserData $data){
$this->to= $data->to;
$this->uid= $data->uid;
$this->username= $data->username;
$this->password= $data->password;
$this->setSubject();
$this->setBody();
}
public function setBody(){
var_dump($this);
}
protected function setSubject(){}
}
$o= new RegisterMail();
$o->setUserData(new RegisterUserData('to@site.com',md5(time()),'john','red'));
?>
Ps.
In the script I use the RegisterMail object by aggregation
like this
class RegisterController{
public function __construct(RegisterMail $mail) {
$this->controller($mail);
}
public controller($mail){
//retieve post value validation insert - set and send email - view
}
}
Bye.