Laravel命令不能调用$this->;子类中的info()


Laravel command cannot call $this->info() in child class

我刚开始学习PHP中的OO、的基本概念

Foo.php

class Foo extends Command {

    public function __construct()
    {
        parent::__construct();
    }
    public function fire()
    {
        $bar = new Bar();
    }
}

Bar.php

class Bar extends Foo {
    public function __construct()
    {
        parent::__construct();
        $this->info('Bar');
    }
}

当我运行Foo::fire()时,它给出:Call to undefined method Foo::__construct()。但是Foo显然有一个构造函数,我做错了什么?

我怀疑的另一件事是,这可能是一个Laravel问题,而不是PHP问题。这是我创建的artisan命令。

编辑:

同时在Bar中的任何位置调用$this->info('Bar')也将给出Call to a member function writeln() on a non-object。为什么我不能从子类调用父类的方法?

我也遇到了这个问题,觉得Marcin的反馈很冷淡,毫无帮助,尤其是在他的评论中。为此,我很高兴向你和任何其他遇到这个问题的人提供这个答案。

在原来的类栏:

class Bar extends Foo {
     public function __construct()
     {
        parent::__construct();
        $this->info('Bar');
    }
}

我只需要设置"输出"的属性,如下所示:

class Bar extends Foo {
     public function __construct()
     {
        parent::__construct();
        $this->output = new Symfony'Component'Console'Output'ConsoleOutput();
        $this->info('Bar');
    }
}

希望这有帮助!