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

0

ファイル「class.emailer.php」で行うだけです

public $recipients = array();

ちょっと裸の代わりに

public $recipients;

以上で、余剰として、必要に応じてコンストラクターを自由に埋めることができます。- あれについてどう思う?

よろしく、M.

于 2013-05-01T23:44:13.083 に答える