非静态方法 ..... 不应静态调用 [英] Non-static method ..... should not be called statically

查看:58
本文介绍了非静态方法 ..... 不应静态调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近更新了 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();

这篇关于非静态方法 ..... 不应静态调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆