2014-11-02 5 views
6

Sto solo agli inizi con la OO concetti di base 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'); 

    } 
} 

Quando eseguo Foo::fire() dà: Call to undefined method Foo::__construct(). Ma lo Foo ha chiaramente un costruttore, cosa sto facendo male?

Un'altra cosa che sospetto è che potrebbe trattarsi di un problema di Laravel piuttosto che di PHP. Questo è un comando artisan che ho creato.

EDIT:

chiamando anche $this->info('Bar') qualsiasi parte Bar darà anche Call to a member function writeln() on a non-object. Perché non posso chiamare il metodo di un genitore dalla classe figlio?

+0

Questo è stato un approccio migliore per me: https://stackoverflow.com/a/41122816/470749 E sto anche indagando su https://stackoverflow.com/a/38064494/470749 per vedere se questo è il miglior modo. – Ryan

risposta

11

Mi sono anche imbattuto in questo problema e ho sentito che il feedback di Marcin era freddo e inutile, specialmente nei suoi commenti. Per questo sono felice di rispondere con questa risposta a te ea chiunque altro incappi in questo problema.

Nella classe Bar originale:

class Bar extends Foo { 

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

Avevo solo bisogno di impostare la proprietà di 'uscita' simile al seguente:

class Bar extends Foo { 

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

Spero che questo è utile!

+1

Ho trovato 'use Symfony \ Component \ Console \ Output \ ConsoleOutput;' nella parte superiore del file e quindi '$ this-> output = new ConsoleOutput;' ha funzionato per me – Sevenearths

+1

Sarebbe bello se Laravel avesse effettivamente verificato che lì non era un oggetto '$ output' e ha dato un messaggio di errore appropriato, invece di lasciare che PHP lanciasse un errore. Soprattutto perché questo è un nuovo comportamento. – andrewtweber

Problemi correlati