Skip to content

Commit c5879cd

Browse files
authored
Update README.md
1 parent f71acd4 commit c5879cd

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Async, event-driven and UTF-8 aware standard console input & output (STDIN, STDO
1717
* [Advanced](#advanced)
1818
* [Stdout](#stdout)
1919
* [Stdin](#stdin)
20+
* [Pitfalls](#pitfalls)
2021
* [Install](#install)
2122
* [License](#license)
2223
* [More](#more)
@@ -324,6 +325,34 @@ You can access the current instance through the [`Stdio`](#stdio):
324325
$stdin = $stdio->getInput();
325326
```
326327

328+
## Pitfalls
329+
330+
The [`Readline`](#readline) has to redraw the current user
331+
input line whenever output is written to the `STDOUT`.
332+
Because of this, it is important to make sure any output is always
333+
written like this instead of using `echo` statements:
334+
335+
```php
336+
// echo 'hello world!' . PHP_EOL;
337+
$stdio->write('hello world!' . PHP_EOL);
338+
```
339+
340+
Depending on your program, it may or may not be reasonable to
341+
replace all such occurences.
342+
As an alternative, you may utilize output buffering that will
343+
automatically forward all write events to the [`Stdio`](#stdio)
344+
instance like this:
345+
346+
```php
347+
ob_start(function ($chunk) use ($stdio) {
348+
// forward write event to Stdio instead
349+
$stdio->write($chunk);
350+
351+
// discard data from normal output handling
352+
return '';
353+
}, 1);
354+
```
355+
327356
## Install
328357

329358
The recommended way to install this library is [through Composer](https://getcomposer.org).

0 commit comments

Comments
 (0)