我正在尝试创建一个自定义类,它将为我处理邮件。
这是我的班级(mailerclass.php):
class Mailer {
// Private fields
private $to;
private $subject;
private $message;
// Constructor function
function __construct($to, $subject, $message) {
$to = $to;
$subject = $subject;
$message = $message;
}
// Function to send mail with constructed data.
public function SendMail() {
if (mail($to, $subject, $messagecontent)) {
return "Success";
}
else {
return "Failed";
}
}
}当我试图在这里调用它(index.php)时,我得到了“调用未定义函数SendMail()”消息?
if ($_SERVER['REQUEST_METHOD'] == "POST") {
// Import class
include('mailerclass.php');
// Trim and store data
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$message = trim($_POST['message']);
// Store mail data
if ($validated == true) {
// Create new instance of mailer class with user data
$mailer = new Mailer($to, $subject, $message);
// Send Mail and store result
$result = $mailer.SendMail();
}为什么会发生这种事?
发布于 2015-06-01 09:51:04
您不使用点调用类方法。使用->调用类方法(而不是静态方法),如下所示:
$result = $mailer->SendMail();此外,还需要使用$this-> (如果不是静态的话)设置属性,将构造函数的内容更改为:
$this->to = $to;
$this->subject = $subject;
$this->message = $message;mail()函数也是如此:
mail($this->to, $this->subject, $this->messagecontent)您见过我多次提到静态,如果您想访问类中的静态属性或方法,可以使用self::。
发布于 2015-06-01 09:50:58
点.用于concatenate。使用->访问类成员
$result = $mailer->SendMail();发布于 2015-06-01 09:52:46
.是一个联系人操作符,要访问变量和函数的类成员,请使用->操作符使用以下代码:
$result = $mailer->SendMail();
https://stackoverflow.com/questions/30570323
复制相似问题