dongpan2788 2019-05-15 00:11
浏览 145
已采纳

为什么我不能在工匠命令类'__construct方法中访问/使用$ this->选项('debug')?

I am trying to create a artisan command with a --debug option in laravel. To use the debug switch within the whole class scope I use the __construct and assign it to a class property.

    public function __construct(){
        parent::__construct();

        $this->debug = $this->option('debug');
    } 

there is no problem when I use $this->option('debug') within the handle method. But when I use it in the __construct then I get

Call to a member function getOption() on null {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Call to a member function getOption() on null at /Users/user/Sites/app/vendor/laravel/framework/src/Illuminate/Console/Command.php:310)

I am not sure but it looks like as if the option() is not ready at construct time. Checked the document but could't find anything. Is there any one who knows something about it or a way how I could accomplish that?

  • 写回答

1条回答 默认 最新

  • douning5041 2019-05-15 00:23
    关注

    It seems like you can't access any of the InputInterface's methods because they're just not initialised at this point.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?