Non-static method … should not be called staticall

2019-01-09 03:15发布

I have recently done an update to PHP 5.4, and I get an error about static and non-static code.

This is the error:

PHP Strict Standards:  Non-static method VTimer::get() 
should not be called statically in /home/jaco/public_html/include/function_smarty.php on line 371

This is the line 371:

$timer  = VTimer::get($options['magic']);

I hope somebody can help.

2条回答
老娘就宠你
2楼-- · 2019-01-09 03:33

You can also change the method to be static like so:

class Handler {
    public static function helloWorld() {
        echo "Hello world!";
    }
}
查看更多
Root(大扎)
3楼-- · 2019-01-09 03:35

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 initialization 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();
查看更多
登录 后发表回答