PHP 错误:致命错误:调用未定义的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20023112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:48:09  来源:igfitidea点击:

PHP Error: Fatal error: Call to undefined method

phpmethodsundefinedfatal-error

提问by Akshay Kalose

I'm having a problem with my Mail class. It was working before, but now i'm not sure what happened. here is the error:

我的邮件类有问题。它以前有效,但现在我不确定发生了什么。这是错误:

Fatal error: Call to undefined method Mail::sendTo() in C:\...\web\modules\register.php on line 30

My mail class:

我的邮件类:

class Mail
{
public static $Headers = 'From:[email protected]';
public $sendtowho;
public $subject;
public $message;
public $template;

public function sendTo($who='')
{
    $this->sendtowho = $who;
}

public function with($subj='',$template)
{
    $this->subject = $subj;
    $this->template = $template;
}

public function addVars($variables)
{
    $TemplateHandler = new Template('mail');
    $this->message = $TemplateHandler->renderContent($this->template, $variables);
}

public function send()
{
    mail($this->sendtowho, $this->subject, $this->message, self::$Headers);
}
}

My register.php

我的注册.php

$mail = new Mail();
$mail->sendTo(User::getMailFromUsername($username));
$mail->with(' Registration Info','registration');
$mail->addVars(array('name' => User::getNameFromUsername($username), 'regKey' => $regKey));
$mail->send();

Line where the error is happening:

发生错误的行:

$mail->sendTo(User::getMailFromUsername($username));

$mail->sendTo(User::getMailFromUsername($username));

I'd appreciate any help, thanks!

我很感激任何帮助,谢谢!

EDIT: Made some change to names of method and var to, so you can understand it better. BUT STILL GIVING SAME ERROR!!

编辑:对方法和 var 的名称进行了一些更改,以便您可以更好地理解它。但仍然给出同样的错误!!

回答by Akshay Kalose

I fixed the problem. Just need to change the name of my class from Mail to MailInterface. Mail class is already taken by something else. I am using XAMPP with PHP 5.5.

我解决了这个问题。只需要将我的类的名称从 Mail 更改为 MailInterface。邮件类已经被别的东西占用了。我在 PHP 5.5 中使用 XAMPP。