Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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:

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

I hope somebody can help.

like image 807
Novice Hobby PHP Boy Avatar asked Oct 30 '13 21:10

Novice Hobby PHP Boy


People also ask

Can we call non static method from static?

A static method can call only other static methods; it cannot call a non-static method.

What are non static methods called?

Constructor is a special method which in theory is the "only" non-static method called by any static method.

Can we call non static method from Main?

Since you want to call a non-static method from main, you just need to create an object of that class consisting non-static method and then you will be able to call the method using objectname.

How can call non static method from static method in laravel?

When you use Model::method() is called a static method. So the method should read: public static function method() In your case either add the static key word or inject the model in your controller function. Then call it like: $model->method().


2 Answers

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. 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:

(new ClassName)->method(); 
like image 195
mamdouh alramadan Avatar answered Oct 05 '22 18:10

mamdouh alramadan


You can also change the method to be static like so:

class Handler {     public static function helloWorld() {         echo "Hello world!";     } } 
like image 32
deadghost Avatar answered Oct 05 '22 16:10

deadghost