Constructor.
public __construct ( string | null $name = null ) | ||
$name | string | null | The name of the command; passing null means it must be set in configure() |
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MyCommand extends Command { protected function configure() { $this ->setName('my:command') ->setDescription('My command description') ->addArgument('argument', InputArgument::REQUIRED, 'Argument description'); } protected function execute(InputInterface $input, OutputInterface $output) { $argument = $input->getArgument('argument'); // Do something with $argument $output->writeln('Command executed successfully.'); } }
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class MyCommand extends Command { public function __construct() { parent::__construct('my:command'); $this->setDescription('My command description'); $this->addArgument('argument', InputArgument::REQUIRED, 'Argument description'); } protected function execute(InputInterface $input, OutputInterface $output) { $argument = $input->getArgument('argument'); // Do something with $argument $output->writeln('Command executed successfully.'); } }In this example, we use the `__construct` method to configure the command instead of overriding the `configure` method. We call the parent constructor with the name "my:command" and then set the description and the required "argument" using method chaining. Package library: `symfony/console`
public __construct ( string | null $name = null ) | ||
$name | string | null | The name of the command; passing null means it must be set in configure() |