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