非静态方法.....不应静态调用 [英] Non-static method ..... should not be called statically
问题描述
我最近对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']);
static
和non-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();
这篇关于非静态方法.....不应静态调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!