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

新手上路,请多包涵

我最近对 PHP 5.4 进行了更新,我收到关于静态和非静态代码的错误。

这是错误:

 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']);

我希望有人能帮忙。

原文由 Novice Hobby PHP Boy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 266
1 个回答

这意味着它应该被称为:

$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它并立即调用该方法。像这样:

 ClassName::method();

如果该方法不是静态的,则需要像这样初始化它:

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

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

 (new ClassName)->method();

原文由 mamdouh alramadan 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题