值不在类的公共字段中赋值

时间:2017-06-19 13:13:12

标签: php laravel class laravel-5

我有一个班级class DashBoardController extends Controller { public $str; public function __construct($str) { $this->str = $str; echo $this->str; } public function dashboard(Request $request) { $obj = new DashBoardController("hello"); die; } }

{{1}}

它给我的例外是

  

类App \ Modules \ User \ Controllers \ DashBoardController中无法解析的依赖项解析[参数#0 [$ str]]

这里有什么错误?

1 个答案:

答案 0 :(得分:1)

如果在加载时没有注入依赖项,则需要传递默认值。

class DashBoardController extends Controller
{
    public $str;

    public function __construct($str='')
    {
        $this->str = $str;
        echo $this->str;
    }

    public function dashboard(Request $request)
    {
        $obj = new DashBoardController("hello");
        die;
    }
}