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

Ske*_*tor 1 php symfony laravel

我正在尝试在 laravel 中创建一个带有 --debug选项的 artisan 命令。要在整个类范围内使用调试开关,我使用 __construct 并将其分配给类属性。

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

        $this->debug = $this->option('debug');
    } 
Run Code Online (Sandbox Code Playgroud)

当我在 handle 方法中使用 $this->option('debug') 时没有问题。但是当我在 __construct 中使用它时,我得到

调用成员函数 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)

我不确定,但看起来好像 option() 在构造时还没有准备好。检查了文档,但找不到任何内容。有没有人对此有所了解或我如何做到这一点?

Dan*_*Dan 8

您似乎无法访问InputInterface 的任何方法,因为此时它们尚未初始化