11

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?

4

1 に答える 1

23

私もこの問題に遭遇し、特に彼のコメントで、Marcin のフィードバックは冷たくて役に立たないと感じました。そのため、この問題に遭遇したあなたや他の人に、この回答を喜んで返信します.

元のクラス Bar では:

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

これが役に立てば幸いです!

于 2014-12-19T18:34:55.527 に答える