非静态方法 ..... 不应静态调用

2022-01-05 00:00:00 static php non-static

我最近更新了 PHP 5.4,但收到关于静态和非静态代码的错误.

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

这是错误:

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

这是第 371 行:

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

希望有人能帮忙.

推荐答案

这意味着它应该被称为:

That means it should be called like:

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

staticnon-static 的区别在于第一个不需要初始化,所以你可以调用 classname 然后追加:: 并立即调用该方法.像这样:

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();

但是,在 PHP 5.4 中,您可以使用以下语法作为简写:

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

(new ClassName)->method();

相关文章