How to not call a function statically in php?

不问归期 提交于 2019-12-23 10:36:58

问题


I am using pear to send mail in PHP. I've followed the example that is on here (http://pear.php.net/manual/en/package.mail.mail.send.php). However, I am getting this error message.

Strict Standards: Non-static method Mail::factory() should not be called statically in C:\xampp\htdocs\functions.php on line 43

So I've been trying to get this Strict Standards message to not show up.

This is my code:

$smtpinfo["host"] = "********";
$smtpinfo["port"] = "587"; 
$smtpinfo["auth"] = true; 
$smtpinfo["username"] = $mail_username; 
$smtpinfo["password"] = $mail_password; 

## This line below is causing the problem ## 
$mail =& Mail::factory("smtp", $smtpinfo);  // <-- Line 43

I've read many Stack Overflow Q&A that say just add a @ to the beginning of $mail. And it is true, it makes the error disappear, but I feel like that just hides the error, and doesn't actually solve the problem.

@$mail =& Mail::factory("smtp", $smtpinfo); 

How do I not call the method above as statically?

Even the documentation on this page (http://pear.php.net/manual/en/package.mail.mail.send.php), says This function cannot be called statically.... but the example they gave is the same way I am calling the method?!

Please don't answer just add @ in front to remove the strict standard or E_ALL & ~E_STRICT ... that is not a solution!


回答1:


If you take a look at the PEAR Mail class, you can see that there are a few instances of it calling methods statically when the methods are not declared as static.

Change line 74 of Mail.php from:

function &factory($driver, $params = array())

to:

static function &factory($driver, $params = array())

The other less desirable alternative would be to modify your php.ini configuration to disregard the E_STRICT warnings, but I believe fixing the cause of the error message is better than hiding it.




回答2:


There is nothing wrong in your code and example in pear documentation page. That is non-fatal message that PHP gives. The main reason of that message is, pear Mail has not been updated for the keyword static. PHP has been introduced static keyword usage in 2006 or 2007, couldn't remember the exact date. PHP gives error about the static calling because of pear Mail 's old code base.




回答3:


without any need to modify the original classes, simply create a custom class with a method:

class Mailer {
    function sendConfirmationEmail() {
        $ret = false;
        $smtp = new Mail();
        $smtp->factory('smtp', $emailServer);
        $mail = $smtp->send($recipient, $headers, $body);

        if (@PEAR::isError($mail)) {
            sprintf("in sendConfirmationEmail error: %s", $mail->getMessage());
        } else {
            sprintf("in sendConfirmationEmail E-Mail successfully sent to: %s", $recipient);
            $ret = true;
        }
        return $ret;
    }

and call it in your static code:

$m = new Mailer();
$m->sendConfirmationEmail();

It worked for me. Hope it helps. Pietro.



来源:https://stackoverflow.com/questions/22050416/how-to-not-call-a-function-statically-in-php

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!