Метод writeln класса Output
Метод writeln класса Output выводит переданную строку
в консоль и автоматически добавляет символ перевода строки
в конце. Первым параметром передаётся текст сообщения.
Вторым необязательным параметром можно указать тип вывода
(например, OutputInterface::OUTPUT_NORMAL или
OutputInterface::OUTPUT_RAW). Метод ничего не возвращает.
Он удобен, когда нужно вывести несколько строк подряд,
не добавляя PHP_EOL вручную.
Синтаксис
$output->writeln(string $message, int $options = 0)
Пример
Давайте выведем приветствие в консоль через команду Symfony:
<?php
namespace AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
class HelloCommand extends Command
{
protected static $defaultName = 'app:hello';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('hello');
return Command::SUCCESS;
}
}
?>
Результат выполнения команды:
hello
Пример
Давайте выведем несколько строк подряд, используя массив:
<?php
namespace AppCommand;
use SymfonyComponentConsoleCommandCommand;
use SymfonyComponentConsoleInputInputInterface;
use SymfonyComponentConsoleOutputOutputInterface;
class HelloCommand extends Command
{
protected static $defaultName = 'app:hello';
protected function execute(InputInterface $input, OutputInterface $output): int
{
$messages = ['article', 'user', 'abcde'];
foreach ($messages as $message) {
$output->writeln($message);
}
return Command::SUCCESS;
}
}
?>
Результат выполнения команды:
article
user
abcde