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

后端 未结 4 750
攒了一身酷
攒了一身酷 2020-12-03 16:33

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:

相关标签:
4条回答
  • 2020-12-03 17:19

    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();
    
    0 讨论(0)
  • 2020-12-03 17:26

    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.

    0 讨论(0)
  • 2020-12-03 17:26

    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!

    0 讨论(0)
  • 2020-12-03 17:27

    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.

    0 讨论(0)
提交回复
热议问题