How to Solve "Non-Static Method Xxx:Xxx() Should Not Be Called Statically in PHP 5.4

Non-static method ..... should not be called statically

That means it should be called like:

$timer = (new VTimer)->get($options['magic']);

The difference between static and non-static is that the first one doesn't need instantiation so you can call the classname then append :: to it and call the method immediately.
Like so:

ClassName::method();

and if the method is not static you need to initialize it like so:

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

However, in PHP >=5.4 you can use this syntax instead as a shorthand:

(new ClassName)->method();

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

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.

Strict Standards: Non-static method should not be called statically, assuming $this from incompatible context

Just change your method type, add the static keyword and call as you are doing now.

public function getMapper() {
if (null === self::$__mapper)
{
self::setMapper(new Vox_Model_Setting_Mapper());
}
return self::$__mapper;
}

to

public static function getMapper() { # see extra static keyword
if (null === self::$__mapper)
{
self::setMapper(new Vox_Model_Setting_Mapper());
}
return self::$__mapper;
}

Non-static method utf_normalizer::nfc() should not be called statically

go to your /includes folder and open up the startup.php

On line ~22 change the line

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED);

to

error_reporting(E_ALL & ~E_NOTICE & ~E_DEPRECATED & ~E_STRICT);

This may solve your problem =).

Non-static method AJAXChatFileSystem::getFileContents() should not be called statically

Calling a non-static method statically in PHP is deprecated behaviour since version 7.0 and raises an E_DEPRECATED warning. This means that support for this behavour works, but may (and probably will) be removed in a future version.

This behaviour raised an E_STRICT warning in PHP versions 5.*.

Changing your own AJAXChatTemplate::getContent() method to static does not work because it uses $this which only makes sense in the context of an instance of a class. Therefore it triggers a fatal error in a static context.

You are using the AJAX-Chat library—you haven't stated what version you are using but there is an issue that discusses the error you encounterd.

In line with this reported issue, a commit to recent versions of this library were made to change this a static behaviour.


To resolve your issue, you have two choices:

Continue to use the version of AJAX-Chat that you have currently installed

Just use the AJAXChatFileSystem::getFileContents() non-statically. Create an instance of the class and use that by modifying your getContent() method like so:

function getContent()
{
if (!$this->_content) {
$ajaxChatFileSystem = new AJAXChatFileSystem();
$this->_content = $ajaxChatFileSystem->getFileContents($this->_templateFile);
}

return $this->_content;
}

Upgrade to the latest version of this library and use the static method

There does not appear to be a changelog, so you should test your code wherever you use AJAX-Chat to ensure there are no breaking changes.


Technically, you have a third choice: since this is an E_DEPRECATED warning—implying that the functionality is flagged for removal at a future date—you can safely ignore this warning, for now.

E_DEPRECATED warnings (as with all notices, warnings and errors) should be disabled from display to the user in production code.

However, I do not recommend this because your logs will be full of E_DEPRECATED warnings. Moreover, as already mentioned, future versions of PHP may remove support for calling non-static methods statically.

Hope this helps :)

Error message Strict standards: Non-static method should not be called statically in php

Your methods are missing the static keyword. Change

function getInstanceByName($name=''){

to

public static function getInstanceByName($name=''){

if you want to call them statically.

Note that static methods (and Singletons) are death to testability.

Also note that you are doing way too much work in the constructor, especially all that querying shouldn't be in there. All your constructor is supposed to do is set the object into a valid state. If you have to have data from outside the class to do that consider injecting it instead of pulling it. Also note that constructors cannot return anything. They will always return void so all these return false statements do nothing but end the construction.

PhpStorm - How to detect PHP error Non-static method should not be called statically?

That inspection works fine here (proof).

  1. Please try Code | Inspect Code... on this file -- it will force re-analysing of this file from scratch. Any better?

  2. If nothing -- please do File | Invalidate Caches... and restart IDE


P.S.

If you are interested of running this inspection only on whole project -- use Code | Run Inspection by Name... -- it's much faster that doing full Inspect Code for each file.



Related Topics



Leave a reply



Submit