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

Hel*_*Che 9 php class-constructors laravel

我刚刚开始使用PHP中的基本概念OO,

Foo.php

class Foo extends Command {


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

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

}
Run Code Online (Sandbox Code Playgroud)

Bar.php

class Bar extends Foo {

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

    }
}
Run Code Online (Sandbox Code Playgroud)

当我跑Foo::fire()它时:Call to undefined method Foo::__construct().但Foo显然有一个构造函数,我做错了什么?

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

编辑:

同时$this->info('Bar')在任何地方打电话Bar也会给Call to a member function writeln() on a non-object.为什么我不能从子类中调用父类的方法?

ala*_*ock 18

我也遇到了这个问题,并且觉得Marcin的反馈很冷淡无益,特别是在他的评论中.为此我很乐意回答你和其他任何偶然发现这个问题的人.

在原来的班级栏中:

class Bar extends Foo {

     public function __construct()
     {
        parent::__construct();
        $this->info('Bar');
    }
}
Run Code Online (Sandbox Code Playgroud)

我只需要设置'output'的属性,如下所示:

class Bar extends Foo {

     public function __construct()
     {
        parent::__construct();
        $this->output = new Symfony\Component\Console\Output\ConsoleOutput();
        $this->info('Bar');
    }
}
Run Code Online (Sandbox Code Playgroud)

希望这有用!

  • 我在文件顶部找到了“使用Symfony \ Component \ Console \ Output \ ConsoleOutput;”,然后“ $ this-> output = new ConsoleOutput;”对我有用 (2认同)
  • 如果Laravel实际上检查不存在$ output对象并给出适当的错误消息,而不仅仅是让PHP抛出错误,那就太好了。特别是因为这是新行为。 (2认同)