0

我写了一个这样的课程: -

class Mail{
    function register()
    {
    return array('message_subject'=>$message_subject,'message_content'=>$message_content);
    }
}

然后在主页中,我尝试了:-

echo Mail::register();

主页给了我“数组”的值。

我该如何修改 Mail::register(); 以便它可以分别接收“message_subject”和“message_content”?

4

3 回答 3

0

带参数

class Mail{
    function register($message_subject, $message_content)
    {
        return array('message_subject'=>$message_subject,'message_content'=>$message_content);
    }
}

echo Mail::register('foo', 'bar')

或使用成员字段

class Mail {
  private $message_subject;
  private $message_content;

  public function __construct($subject, $content) {
    $this->message_subject = $subject;
    $this->message_content = $content;
  }

  function register() {
    return array('message_subject' => $this->message_subject,'message_content' => $this->message_content);
  }
}

$mail = new Mail('foo', 'bar');
$mail->register();
于 2013-05-19T13:49:11.867 回答
0

你可以试试

$mail = new Mail();
list($subject, $content) = $mail->register();

或者

$mail = new Mail();
$data = $mail->register();
printf("%s \n %s", $data['message_subject'], $data['message_content']);

我猜你的课是这样的:

class Mail {
    private $subject, $content;

    function __construct() {
        $this->subject = "Demo";
        $this->content = "Hello World";
    }

    function register() {
        return array(
                'message_subject' => $this->subject,
                'message_content' => $this->content
        );
    }
}
于 2013-05-19T13:50:14.507 回答
0

将 Mail::register() 分配给一个变量,然后打印它,因为您正在从中重新计算一个数组。数组总是由 print_f 而不是 echo 打印

$mail = Mail::register();
echo'<pre>';
print_r($mail);
echo '</pre>';
?>
于 2013-05-19T13:46:44.857 回答