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

前端之家收集整理的这篇文章主要介绍了php – Laravel命令不能在子类中调用$this-> info()前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我刚刚开始使用 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()时,它给出:调用未定义的方法Foo :: __ construct().但是Foo显然有一个构造函数,我做错了什么?

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

编辑:

在Bar中的任何地方调用$this-> info(‘Bar’)也会在非对象上调用成员函数writeln().为什么我不能从子类中调用父类方法

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

在原来的班级栏中:

class Bar extends Foo {

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

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

class Bar extends Foo {

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

希望这有用!

猜你在找的Laravel相关文章