32

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?

hakre
  • 193,403
  • 52
  • 435
  • 836
kaese
  • 10,249
  • 8
  • 29
  • 35
  • Create an instance of Config and call getData() from it – Musa May 26 '12 at 18:06
  • Could you include a sample of the contents of `&getData()`? Specifically, what exactly is being returned and how. – Ayman Safadi May 26 '12 at 18:09
  • Added the contents of &getData() to the original question -- I must emphasise though, that this error has only been raised since the upgrade to 5.4, so I'm confident the logic is fine – kaese May 26 '12 at 18:17
  • possible duplicate of [Error message Strict standards: Non-static method should not be called statically in php](http://stackoverflow.com/questions/4684454/error-message-strict-standards-non-static-method-should-not-be-called-staticall) – Michel Ayres Feb 14 '14 at 16:45

4 Answers4

44

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.

lanzz
  • 42,060
  • 10
  • 89
  • 98
  • Thanks lanzz - that's my thought too. I'm hesitant to make them purely static because I'm unsure if they're called from a non-static context too. I'll try removing E_STRICT from error_reporting() and get back to you. Thanks. – kaese May 26 '12 at 18:18
  • With your getData() implementation, there would be no need to call in non-static context, as it does not manipulate instance data in any way; and if it did work with instance data, you won't be able to call it in static context. – lanzz May 26 '12 at 18:25
  • Thanks lanzz - however there are other methods throwing the same warning which may be called non-statically. Your suggestion to simply turn off the strict errors in the error_reporting() seems to have suppressed the issue. Thanks! – kaese May 26 '12 at 18:27
  • 5
    Calling non-static functions in a static context are now deprecated as of PHP 5.6, and the functionality will be removed altogether in a future version, so this should be discouraged completely. – Matthew G Sep 14 '14 at 04:10
  • Gracias por tus comentarios, he probado esto y me funcionó /*error_reporting(E_ALL);*/ – Neftali Acosta Jul 17 '17 at 21:04
31

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();
volkinc
  • 2,143
  • 1
  • 15
  • 19
10

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!

Bruno Campos
  • 405
  • 7
  • 6
-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.

  • 4
    This will hide all errors. It's a good idea to not show errors to the user for security considerations, however they might still appear in log files or when developing. Other, real, problems may go unnoticed with this approach. – amoebe Jun 13 '15 at 12:37
  • This will not resolve any problems and will make other issues harder to detect. – Robin Layfield Nov 25 '15 at 23:54
  • On a busy website, you may have 100s of MB of log data (`error_log` file) every day if you simply not display them (in the HTML file/to the client) but still enable them (be `E_STRICT` in PHP). Happened to me, too. – Ned64 Dec 30 '15 at 10:39