2

addRecipients 使用 push_array 但它不起作用。我在这里做错了什么?

在 class.emailer.php 中

class Emailer
{
public $sender;
public $recipients;
public $subject;
public $body;

function __construct($sender)
{
    $this->sender = $sender;
    $this->recipients = array();
}

public function addRecipients($recipient)
{
    array_push($this->recipients, $recipient);
}

public function setSubject($subject)
{
    $this->subject = $subject;
}

public function setBody($body)
{
    $this->body = $body;
}

public function sendEmail()
{
    echo "From sendEmail, send email initiated<br />";
    echo "Body: ".$this->body."<br />";
    echo "Subject: ".$this->subject."<br />";
    print_r ($this->sender);
    print_r ($this->recipients);
    echo "<br />";
    foreach ($this->recipients as $recipient)
    {
        echo "6<br />";
        //$result = mail($recipient, $this->subject, $this->body,"From: {$this->sender}\r\n");
        echo "7<br />";
        if ($result) echo "Mail successfully sent to {$recipient}<br/>";
    }
}

}

在 class.extendedemailer.php 中

include_once("class.emailer.php"); 

class ExtendedEmailer extends emailer
{
function  __construct(){
    //overwriting __contruct here   
}

public function setSender($sender)
{
    $this->sender = $sender;
}
}

在 sendemail.php 中

    include_once("class.extendedemailer.php"); 
echo "start 1<br />";
$xemailer = new ExtendedEmailer(); 

echo "1. adding sender: <br />";
$xemailer->setSender("sender@mywebsite.com"); 
print_r ($xemailer->sender);
echo "<br />2. adding recipients: <br />";
$xemailer->addRecipients("recipientemail@gmail.com"); 
var_dump ($xemailer->recipients);
echo "<br />3. adding subject: <br />";
$xemailer->setSubject("Just a Test<br />"); 
print_r ($xemailer->subject);
echo "4. adding body<br />";
$xemailer->setBody("Hi, How are you?<br />"); 
print_r ($xemailer->body);
echo "5. sending email<br />";
$xemailer->sendEmail();

收件人的输出为 NULL。

start 1
1. adding sender: 
sender@mywebsite.com
2. adding recipients: 
NULL 
3. adding subject: 
Just a Test
4. adding body
Hi, How are you?
5. sending email
From sendEmail, send email initiated
Body: Hi, How are you?

Subject: Just a Test
4

5 回答 5

4

$recipients当您重写构造函数时,您可能没有启动。

class Emailer {
    public $recipients = array();

    function __construct($sender)
    {
        $this->sender = $sender;
    }

应该这样做。检查您的警告

警告:array_push() 期望参数 1 为数组,在 ...

于 2013-05-01T23:09:18.327 回答
2

您需要从 ExtendedEmailer 的构造函数中调用父级的构造函数。

class ExtendedEmailer extends emailer
{
  function  __construct(){
    parent::__construct(null); 
  }

  // ...
}

否则$recipients永远不会初始化为数组。

于 2013-05-01T23:12:39.853 回答
2

问题是你重写了Email类的构造函数并且从不调用它(parent::__construct())。在类构造函数中调用父构造函数,或者如果您实际上没有使用它(在您的示例代码中,您没有使用),则ExtendedEmailer完全删除该构造函数。ExtendedEmailer

于 2013-05-01T23:15:02.713 回答
1

您错过了在子构造函数中调用父构造函数

class ExtendedEmailer extends emailer
{
    function  __construct(){
        //overwriting __contruct here
        parent::__construct('dummy_sender@sender.com');
    }
}

应该解决这个问题。

于 2013-05-01T23:15:33.437 回答
0

在文件“class.emailer.php”中做

public $recipients = array();

而不仅仅是有点赤裸

public $recipients;

仅此而已,此外,您还可以根据需要随意填充构造函数。- 你怎么看?

问候,M。

于 2013-05-01T23:44:13.083 回答