Laravel命令无法在子类中调用$ this-> info() [英] Laravel command cannot call $this->info() in child class

查看:39
本文介绍了Laravel命令无法在子类中调用$ this-> info()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是从PHP的基本概念OO开始,

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

    }
}

当我运行 Foo :: fire()时,它给出:对未定义方法Foo :: __ construct()的调用.但是 Foo 显然有一个构造函数,我在做什么错了?

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

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

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

也可以在 Bar 中的任何地方调用 $ this-> info('Bar'),也会在非-对象.为什么我不能从子类中调用父方法?

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?

推荐答案

我也遇到了这个问题,并且感到Marcin的反馈是冷漠无助的,尤其是在他的评论中.为此,我很高兴对您和其他在此问题上迷路的人作出答复.

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.

在原始班级Bar:

class Bar extends Foo {

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

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

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

希望这会有所帮助!

这篇关于Laravel命令无法在子类中调用$ this-> info()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆