title: Laravel Call the methods of a Artisan Command from a Parent Class
body: <p>I was wondering if you could help me with the problem that I found a few hours ago and I have not been able to solve it. I happen to be trying to develop a small package in <code>Laravel</code>. This package will contain several <code>commands</code>, but for now I need those <code>commands</code> to inherit some methods and properties from a <code>parent class</code>. I have defined the following structure in my project:</p>
  <pre><code>package
  ├── composer.json
  └── src
      ├── Commands
      │   └── ACommand.php
      │   └── BCommand.php
      ├── Package.php
      └── PackageServiceProvider.php
  </code></pre>
  <p>I register the commands inside the <code>PackageServiceProvider</code> as follows:</p>
  <pre><code>&lt;?php
  
  namespace Author\Package;
  
  use Illuminate\Support\ServiceProvider;
  
  class PackageServiceProvider extends ServiceProvider
  {
      /**
       * The commands to be registered.
       *
       * @var array
       */
      protected $commands = [
          \Author\Package\Commands\ACommand::class,
          \Author\Package\Commands\BCommand::class,
      ];
  
      /**
       * Register any application services.
       *
       * @return void
       */
      public function register()
      {
          //
      }
  
      /**
       * Bootstrap any application services.
       *
       * @return void
       */
      public function boot()
      {
          $this-&gt;commands($this-&gt;commands);
      }
  }
  
  </code></pre>
  <p>Let's take the <code>ACommand</code> command as an example, it contains the following very basic structure to be brief:</p>
  <pre><code>&lt;?php
  
  namespace Author\Package\Commands;
  
  use Author\Package\Package;
  use Illuminate\Console\Command;
  
  class ACommand extends Command
  {
      /**
       * The name and signature of the console command.
       *
       * @var string
       */
      protected $signature = 'command:name';
  
      /**
       * The console command description.
       *
       * @var string
       */
      protected $description = 'Command description';
  
      /**
       * Create a new command instance.
       *
       * @return void
       */
      public function __construct()
      {
          parent::__construct();
      }
  
      /**
       * Execute the console command.
       *
       * @return int
       */
      public function handle(Package $package)
      {
          return 0;
      }
  }
  </code></pre>
  <p>As you can see, in the <code>handle()</code> method I do <code>dependency injection</code> to the <code>Package</code> class. This is the content of the <code>Package</code> class:</p>
  <pre><code>&lt;?php
  
  namespace Author\Package;
  
  use Illuminate\Console\Concerns\InteractsWithIO;
  
  class Package
  {
      use InteractsWithIO;
  
      public function __construct()
      {
          $this-&gt;info(&quot;Hello World!&quot;);
      }
  }
  
  </code></pre>
  <p>when I run the command:</p>
  <pre><code>php artisan command:name
  </code></pre>
  <p>I get the following error in the terminal:</p>
  <pre><code>Call to a member function writeln() on null
  </code></pre>
  <h2>What other kinds of things have I tried?</h2>
  <ol>
  <li>Create a <code>base command</code> and <code>extend</code> the other commands to that <code>class</code>, it would be as follows:</li>
  </ol>
  <pre><code>package
  ├── composer.json
  └── src
      ├── Commands
      │   └── ACommand.php
      │   └── BCommand.php
      │   └── BaseCommand.php
      └── PackageServiceProvider.php
  </code></pre>
  <p>Let's take the <code>ACommand</code> command as an example here:</p>
  <pre><code>&lt;?php
  
  namespace Author\Package\Commands;
  
  use Illuminate\Console\Command;
  
  class ACommand extends BaseCommand
  {
      /**
       * The name and signature of the console command.
       *
       * @var string
       */
      protected $signature = 'command:name';
  
      /**
       * Create a new command instance.
       *
       * @return void
       */
      public function __construct()
      {
          parent::__construct();
      }
  
  ...
  </code></pre>
  <p>the content of <code>BaseCommand</code> in this case I have left it like this:</p>
  <pre><code>&lt;?php
  
  namespace Author\Package\Commands;
  
  use Author\Package\Package;
  use Illuminate\Console\Command;
  
  abstract class BaseCommand extends Command
  {
      /**
       * Create a new command instance.
       *
       * @return void
       */
      public function __construct()
      {
          parent::__construct();
  
          $this-&gt;info(&quot;Hello World!&quot;);
      }
  }
  </code></pre>
  <p>and I get the same error as a result:</p>
  <pre><code>Call to a member function writeln() on null
  </code></pre>
  <ul>
  <li><em>Also setting the <code>BaseCommand</code> class as <code>concrete</code> and not as <code>abstract</code> I get the same error.</em></li>
  </ul>
  <p>Basically for now what I need is to do almost all my logic from the <code>__construct()</code> method in a parent class .. and leave the result in a <code>property</code>. Then from a <code>child class</code>, access that <code>property</code>, since all my commands are going to use that property .. but each one of them is going to do something different with that property.. But it happens that I have not been able to execute statements as I would in a simple <code>command</code>, even when the <code>parent class</code> extends from the class: <code>Illuminate\Console\Command</code>.</p>
  <p>As you can see in the example I am basically trying to display a message in the terminal called from the <code>parent class</code> when the <code>command</code> is executed. But I have not succeeded.
  I have not done <code>Dependency Injection</code> from the <code>__construct()</code> method for the following reason:</p>
  <p><a href="https://laracasts.com/discuss/channels/laravel/all-artisan-command-constructors-are-fired-when-running-any-artisan-commandwhat" rel="nofollow noreferrer">All artisan command constructors are fired when running any artisan command...what?</a></p>
  <p><a href="https://github.com/laravel/ideas/issues/1399" rel="nofollow noreferrer">Change artisan console to only fire constructors for the command being called</a></p>
  <p>As you can see, when <code>Dependency Injection</code> is done in the <code>__construct()</code> method of a <code>command</code>, it is executed in any other <code>command</code>, that is why the documentation advises to do this <code>Dependency Injection</code> in the <code>handle()</code> method.</p>
  <p><a href="https://laravel.com/docs/8.x/artisan#command-structure" rel="nofollow noreferrer">Command Structure</a></p>
  <p>I have also read something here, but it has not served me much:</p>
  <p><a href="https://stackoverflow.com/questions/26698122/laravel-command-cannot-call-this-info-in-child-class">Laravel command cannot call $this-&gt;info() in child class</a></p>
  <p>Please if someone expert on the subject could help me. Basically I am trying here, to call methods of the child class (<code>ACommand</code>) from the parent class (<code>Package</code>). I would appreciate any help in advance.</p>