我使用版本 laravel 5.4
在 Helpers/Helper.php
public function test()
{
return 'success';
}
在控制器中
use App\Helpers\Helper;
public function index()
{
$val = Helper::test();
dd($val);
}
錯誤:不應靜態呼叫 Helper::test()
我呼叫了里面的函式helper來使用它。但是得到了上面的錯誤。請告訴我如何在helper. 謝謝
uj5u.com熱心網友回復:
要test靜態呼叫該函式,您必須將其定義為靜態函式,如下所示。否則,(new Helper())->test();對于不需要訪問$this. 您可以在 PHP 手冊https://www.php.net/manual/en/language.oop5.static.php 中閱讀更多有關使用靜態方法的資訊
namespace App\Helpers;
class Helper
{
public static function test()
{
return 'success';
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/370943.html
下一篇:將多行輸出為多列
