Hi.
I've got a snippet like this:
<?php
abstract class UserMail{
protected $to= '';
protected $uid= '';
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($to,$uid);
}
class ResetMail extends UserMail{
public function __construct() {
parent::__construct();
}
public function setUserData($to,$uid){
$this->to= $to;
$this->uid= $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($to,$uid,$username,$password=''){
$this->to= $to;
$this->uid= $uid;
$this->username= $username;
$this->password= $password;
$this->setSubject();
$this->setBody();
}
protected function setBody(){// here $this->username and $this->password}
protected function setSubject(){}
}
$o= new RegisterMail();
?>
Is there a way to override the setUserData method in
the RegisterMail class ?
Bye.Is there a way to override the setUserData method in
the RegisterMail class ?
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, but function overloading is not allowed in PHP.
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.Not entirely sure I understand what you want to do. You could define a new class that extends REgisterMail class with the method redefined there, but I've got a feeling that's not what you're asking.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.
<!-- m --><a class="postlink" href="http://it2.php.net/manual/it/language.oop5.overloading.php#77843">http://it2.php.net/manual/it/language.o ... .php#77843</a><!-- m -->
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(<!-- e --><a href="mailto:'[email protected]">'[email protected]</a><!-- e -->',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.Not entirely sure I understand what you want to do. You could define a new class that extends REgisterMail class with the method redefined there, but I've got a feeling that's not what you're asking.
Thanks the same sometimes would be better say to the
forum the goal of the script.
Now I think it shoul be clear to you
If I think of your way again it could be an other way to see the script.
Bye.Concerning member function overloading: yeah, I forgot since I have not used it with PHP, but in any case you cannot do it with the syntax you attempted.
In short I've done this implementation but it doesn't work
How does it not work?Concerning member function overloading: yeah, I forgot since I have not used it with PHP, but in any case you cannot do it with the syntax you attempted.
How does it not work?
Fatal error: Declaration of RegisterMail::setUserData() must be compatible with that of UserMail::setUserData() in F:\www\prova.php on line 73
Fatal error: Declaration of RegisterMail::setUserData() must be compatible with that of UserMail::setUserData() in F:\www\prova.php on line 73
When overriding, the method signature must remain the same. As such, the setUserData() member function in this UserMail class hierarchy should always take a UserData. Unfortunately, this means that it is not very typesafe since you can pass a ResetUserData object to a RegisterMail's setUserData() member function.
Actually, what I had in mind was something along these lines:
<?php
class UserData {
private $to = '';
private $uid = '';
public function __construct($to, $uid) {
$this->to = $to;
$this->uid = $uid;
}
public function to() {
return $this->to;
}
public function uid() {
return $this->uid;
}
}
class ResetUserData extends UserData {
public function __construct($to, $uid) {
parent::__construct($to, $uid);
}
}
class RegisterUserData extends UserData {
private $username = '';
private $password = '';
public function __construct($to, $uid, $username, $password = '') {
parent::__construct($to, $uid);
$this->username = $username;
$this->password = $password;
}
public function username() {
return $this->username;
}
public function password() {
return $this->password;
}
}
abstract class UserMail {
private $data;
public function __construct(UserData $data) {
$this->data = $data;
}
public function send() {
echo $this->data->to();
// ...
}
protected function getData() {
return $this->data;
}
}
class ResetMail extends UserMail {
public function __construct(ResetUserData $data) {
parent::__construct($data);
}
}
class RegisterMail extends UserMail {
public function __construct(RegisterUserData $data) {
parent::__construct($data);
}
}
$register_mail = new RegisterMail(new RegisterUserData(<!-- e --><a href="mailto:'[email protected]">'[email protected]</a><!-- e -->',md5(time()),'john','red'));
$register_mail->send();
?>Yeah,it's far better than mine
Thanks for your help.
Bye.
I've got a snippet like this:
<?php
abstract class UserMail{
protected $to= '';
protected $uid= '';
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($to,$uid);
}
class ResetMail extends UserMail{
public function __construct() {
parent::__construct();
}
public function setUserData($to,$uid){
$this->to= $to;
$this->uid= $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($to,$uid,$username,$password=''){
$this->to= $to;
$this->uid= $uid;
$this->username= $username;
$this->password= $password;
$this->setSubject();
$this->setBody();
}
protected function setBody(){// here $this->username and $this->password}
protected function setSubject(){}
}
$o= new RegisterMail();
?>
Is there a way to override the setUserData method in
the RegisterMail class ?
Bye.Is there a way to override the setUserData method in
the RegisterMail class ?
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, but function overloading is not allowed in PHP.
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.Not entirely sure I understand what you want to do. You could define a new class that extends REgisterMail class with the method redefined there, but I've got a feeling that's not what you're asking.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.
<!-- m --><a class="postlink" href="http://it2.php.net/manual/it/language.oop5.overloading.php#77843">http://it2.php.net/manual/it/language.o ... .php#77843</a><!-- m -->
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(<!-- e --><a href="mailto:'[email protected]">'[email protected]</a><!-- e -->',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.Not entirely sure I understand what you want to do. You could define a new class that extends REgisterMail class with the method redefined there, but I've got a feeling that's not what you're asking.
Thanks the same sometimes would be better say to the
forum the goal of the script.
Now I think it shoul be clear to you
If I think of your way again it could be an other way to see the script.
Bye.Concerning member function overloading: yeah, I forgot since I have not used it with PHP, but in any case you cannot do it with the syntax you attempted.
In short I've done this implementation but it doesn't work
How does it not work?Concerning member function overloading: yeah, I forgot since I have not used it with PHP, but in any case you cannot do it with the syntax you attempted.
How does it not work?
Fatal error: Declaration of RegisterMail::setUserData() must be compatible with that of UserMail::setUserData() in F:\www\prova.php on line 73
Fatal error: Declaration of RegisterMail::setUserData() must be compatible with that of UserMail::setUserData() in F:\www\prova.php on line 73
When overriding, the method signature must remain the same. As such, the setUserData() member function in this UserMail class hierarchy should always take a UserData. Unfortunately, this means that it is not very typesafe since you can pass a ResetUserData object to a RegisterMail's setUserData() member function.
Actually, what I had in mind was something along these lines:
<?php
class UserData {
private $to = '';
private $uid = '';
public function __construct($to, $uid) {
$this->to = $to;
$this->uid = $uid;
}
public function to() {
return $this->to;
}
public function uid() {
return $this->uid;
}
}
class ResetUserData extends UserData {
public function __construct($to, $uid) {
parent::__construct($to, $uid);
}
}
class RegisterUserData extends UserData {
private $username = '';
private $password = '';
public function __construct($to, $uid, $username, $password = '') {
parent::__construct($to, $uid);
$this->username = $username;
$this->password = $password;
}
public function username() {
return $this->username;
}
public function password() {
return $this->password;
}
}
abstract class UserMail {
private $data;
public function __construct(UserData $data) {
$this->data = $data;
}
public function send() {
echo $this->data->to();
// ...
}
protected function getData() {
return $this->data;
}
}
class ResetMail extends UserMail {
public function __construct(ResetUserData $data) {
parent::__construct($data);
}
}
class RegisterMail extends UserMail {
public function __construct(RegisterUserData $data) {
parent::__construct($data);
}
}
$register_mail = new RegisterMail(new RegisterUserData(<!-- e --><a href="mailto:'[email protected]">'[email protected]</a><!-- e -->',md5(time()),'john','red'));
$register_mail->send();
?>Yeah,it's far better than mine
Thanks for your help.
Bye.