Метод write класса Output
Метод write класса Output выводит переданный текст в консоль
и не добавляет перевод строки в конце. Первым параметром передаётся
строка или массив строк для вывода. Вторым параметром можно передать
флаги форматирования, например OutputInterface::OUTPUT_NORMAL.
Синтаксис
public function write(string|iterable $messages, bool $newline = false, int $options = 0): void
Пример
Давайте выведем строку в консоль с помощью метода write:
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ArticleCommand extends Command
{
protected static $defaultName = 'app:article';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->write('hello');
return Command::SUCCESS;
}
}
?>
Результат выполнения команды:
hello
Пример
Давайте выведем несколько строк подряд и сравним с методом writeln:
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ArticleCommand extends Command
{
protected static $defaultName = 'app:article';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->write('abcde');
$output->write('article');
$output->writeln('user');
return Command::SUCCESS;
}
}
?>
Результат выполнения команды:
abcdearticleuser
Пример
Давайте передадим массив строк в метод write:
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class ArticleCommand extends Command
{
protected static $defaultName = 'app:article';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->write(['article 1', 'article 2', 'article 3']);
return Command::SUCCESS;
}
}
?>
Результат выполнения команды:
article 1article 2article 3
Смотрите также
-
класс
SymfonyStyle,
который упрощает вывод в консоль -
метод
writeln,
который выводит строку с переводом строки -
метод
isVerbose,
который проверяет уровень детализации вывода -
метод
setVerbosity,
который задаёт уровень детализации вывода