Home > Software design >  Calling the failed function: 'should not be called statically' in laravel 5.4
Calling the failed function: 'should not be called statically' in laravel 5.4

Time:12-01

I use version laravel 5.4

In Helpers/Helper.php

public function test()
{
  return 'success';
}

In controller

use App\Helpers\Helper;

public function index()
{
  $val = Helper::test();
  dd($val);
}

Error : Helper::test() should not be called statically

I called the function inside helper to use it. But got the error as above. Please show me how to get the function in helper. Thank you

CodePudding user response:

To call the test function statically you must define it as a static function, like below. Otherwise, you would have to do something like (new Helper())->test(); which is probably not something you want to do for a simple helper function that doesn't need to access $this. You can read a bit more about the use of static methods in the PHP manual https://www.php.net/manual/en/language.oop5.static.php

namespace App\Helpers;

class Helper
{
    public static function test()
    {
        return 'success';
    }
}
  • Related