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

I am just starting with the basic concepts of 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 obviously has a constructor, what am I doing wrong?

Another thing I suspect is that it could be a problem with Laravel, not PHP. This is the artisan team that I created.

EDIT:

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

+6
source share
1 answer

I also ran into this problem and felt that Marcin's feedback was cold and unhelpful, especially in his comments. To do this, I am happy to answer this answer to you and everyone who comes across this problem.

In the original Bar class:

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

I just needed to set the 'output' property as follows:

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

Hope this will be helpful!

+11
source

Source: https://habr.com/ru/post/977605/


All Articles