【发布时间】:2015-06-01 09:47:33
【问题描述】:
我正在尝试创建一个自定义类来为我处理邮件。
这是我的课(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();
}
为什么会这样??
【问题讨论】:
-
$mailer->sendMail()。在 php 中,您使用箭头 (->) 而不是点 (.) 调用方法,点 (.) 用于在 php 中连接。 -
从这里开始 php.net/manual/en/language.oop5.php 在 PHP 中你使用 '->' 而不是 '.'访问对象的方法、属性。
标签: php function class methods