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?
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.
class A { public function doSomething(){ //doing something. } } require_once 'A. class. php'; class B { //Call the method doSomething() from the class A. }
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()”.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With