dongpangfu6322 2017-06-19 13:13
浏览 18
已采纳

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

I have a class DashboardController as

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;
    }
}

it gives me Exception as

Unresolvable dependency resolving [Parameter #0 [ $str ]] in class App\Modules\User\Controllers\DashBoardController

What is the error here?

  • 写回答

1条回答 默认 最新

  • douxie7339 2017-06-19 13:43
    关注

    You need to pass default value if there is no dependency injected at the time of load.

    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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Jenkins+k8s部署slave节点offline
  • ¥15 微信小游戏反编译后,出现找不到分包的情况
  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题