简体   繁体   English

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

[英]Non-static method ..... should not be called statically

I have recently done an update to PHP 5.4, and I get an error about static and non-static code.我最近对 ​​PHP 5.4 进行了更新,我收到关于静态和非静态代码的错误。

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:这是第 371 行:

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

I hope somebody can help.我希望有人能帮忙。

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 instantiation so you can call the classname then append :: to it and call the method immediately. staticnon-static的区别在于第一个不需要实例化,因此您可以调用classname名,然后将::附加到它并立即调用该方法。 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:但是,在 PHP 5.4 中,您可以使用以下语法作为简写:

(new ClassName)->method();

You can also change the method to be static like so:您还可以将方法更改为静态,如下所示:

class Handler {
    public static function helloWorld() {
        echo "Hello world!";
    }
}

The most elegant way would be :最优雅的方式是:

(new ClassName)->method();

You can also convert your function to static function call() {} , but that depends on your function and what you're doing with it.您还可以将您的函数转换为static function call() {} ,但这取决于您的函数以及您正在使用它做什么。

If you need to instantiate a class then avoid doing so, treat static functions like constants, they can not have objects and require predefined variables.如果您需要实例化一个类,请避免这样做,将静态函数视为常量,它们不能有对象并且需要预定义的变量。

public function functionName($variable)

改成

public static function functionName($variable)

i have solved this problem in this way.我以这种方式解决了这个问题。

error_reporting('E_NONE');// add this in the pdf dwonload function
$this->load->library('mpdf/mpdf');

This should help.这应该会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM