How can I solve "Non-static method xxx:xxx() should not be called statically in PHP 5.4?

天涯浪子 提交于 2019-12-28 16:30:46

问题


Currently using a large platform in PHP.

The server it's hosted on has recently been upgraded to PHP 5.4.

Since, I've received many error messages like:

[Sat May 26 19:04:41 2012] [error] PHP Strict Standards: Non-static method Config::getData() should not be called statically, assuming $this from incompatible context in /xxx/Config.inc.php on line 35

The example method is defined as (note the lack of 'static' keyword):

function &getData() {
            $configData =& Registry::get('configData', true, null);

    if ($configData === null) {
        // Load configuration data only once per request, implicitly
        // sets config data by ref in the registry.
        $configData = Config::reloadData();
    }

    return $configData;
}

This has no caused a problem before, and I assume the error messages (which cause the application to crash) may be related to the recent upgrade to PHP5.4.

Is there a PHP setting I can modify to 'ignore' the lack of static keyword?


回答1:


You can either remove E_STRICT from error_reporting(), or you can simply make your method static, if you need to call it statically. As far as I know, there is no (strict) way to have a method that can be invoked both as static and non-static method. Also, which is more annoying, you cannot have two methods with the same name, one being static and the other non-static.




回答2:


Disabling the alert message is not a way to solve the problem. Despite the PHP core is continue to work it makes a dangerous assumptions and actions.

Never ignore the error where PHP should make an assumptions of something!!!!

If the class organized as a singleton you can always use function getInstance() and then use getData()

Likse:

$classObj = MyClass::getInstance();
$classObj->getData();

If the class is not a singleton, use

 $classObj = new MyClass();
 $classObj->getData();



回答3:


I don't suggest you just hidding the stricts errors on your project. Intead, you should turn your method to static or try to creat a new instance of the object:

$var = new YourClass();
$var->method();

You can also use the new way to do the same since PHP 5.4:

(new YourClass)->method();

I hope it helps you!




回答4:


I solved this with one code line, as follow: In file index.php, at your template root, after this code line:

defined( '_JEXEC' ) or die( 'Restricted access' );

paste this line: ini_set ('display_errors', 'Off');

Don't worry, be happy...

posted by Jenio.



来源:https://stackoverflow.com/questions/10768576/how-can-i-solve-non-static-method-xxxxxx-should-not-be-called-statically-in

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