Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 838
kaese Avatar asked May 26 '12 18:05

kaese


People also ask

How do you fix non static method Cannot be referenced from a static context?

There is one simple way of solving the non-static variable cannot be referenced from a static context error. In the above code, we have to address the non-static variable with the object name. In a simple way, we have to create an object of the class to refer to a non-static variable from a static context.

How can call non static method from another class in PHP?

class A { public function doSomething(){ //doing something. } } require_once 'A. class. php'; class B { //Call the method doSomething() from the class A. }

How do I make my static method not static?

In the above code we created method staticFunction() inside non-static class "Program". But when we try to call Non static function i.e, TestMethod() inside static function it gives an error - “An object refernce is required for non-static field, member or Property 'Program. TestMethod()”.

What do you mean by non static methods how such methods can be used?

A non-static method in Java can access static methods and variables as follows: A non-static method can access any static method without creating an instance of the class. A non-static method can access any static variable without creating an instance of the class because the static variable belongs to the class.


1 Answers

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.

like image 144
lanzz Avatar answered Oct 01 '22 10:10

lanzz