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

徘徊边缘 提交于 2021-02-20 06:07:06

问题


I'm just starting out with the basic concepts OO in PHP,

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');

    }
}

When I run Foo::fire() it gives: Call to undefined method Foo::__construct(). But Foo clearly has a constructor, what am I doing wrong?

Another thing I suspect is that it might be a Laravel problem rather than PHP. This is an artisan command that I created.

EDIT:

Also calling $this->info('Bar') anywhere in Bar will also give Call to a member function writeln() on a non-object. Why can't I call a parent's method from the child class?


回答1:


I also ran into this issue, and felt Marcin's feedback to be cold and unhelpful, especially in his comments. For that I am happy to respond with this answer to you and anyone else who stumbles on this problem.

In the original class Bar:

class Bar extends Foo {

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

I just needed to set the property of 'output' like the following:

class Bar extends Foo {

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

Hope this is helpful!



来源:https://stackoverflow.com/questions/26698122/laravel-command-cannot-call-this-info-in-child-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!