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

最佳答案

重新编写构造函数时,您可能未在启动$recipients

class Emailer {
    public $recipients = array();

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

应该做。检查您的警告

关于php - 在php中使用array_push方法,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16327847/

10-11 21:56
查看更多