Prior to PHP 7.2+, there is no complete, unified way to find out how input and output flows through PHP console applications.
Current polyfills for PHP versions below 7.2 (such as symfony/polyfill-php72) cannot properly test for a TTY (as there is nothing in PHP core that links userland code to the appropriate functions in unistd.h, the header file that provides access to the POSIX operating system API) - they all perform the same logic that is in isCharacterDevice() as a it's-the-best-that-can-be-done-for-now attempt.
If you need to know if PHP output is going to a TTY, you require PHP 7.2 (and the stream_isatty() function) - however, if you assume that a TTY is the default, and only need to know - for example - if your output is being piped to another process then this implementation is sufficient.
This implementation does provide a isTTY() method, it just falls back to the polyfill method when run on versions of PHP less than 7.2.
$stream = \STDOUT;
$mode = new \Darsyn\FD\StreamMode($stream);
if ($mode->isNamedPipe()) {
$this->setVerbose(false);
}$ php script.php
> STDIN: Character Device
> STDOUT: Character Device
> STDERR: Character Device
# Directing output to /dev/null also reports as "Character Device" so isn't
# enough to determine if output will be seen, discarded, or is a TTY.
# Additionally use stream_isatty() function (available PHP 7.2+) for this.
$ php script.php 2>/dev/null
> STDIN: Character Device
> STDOUT: Character Device
> STDERR: Character Device
$ php script.php | cat
> STDIN: Character Device
> STDOUT: Named Pipe
> STDERR: Character Device
$ echo "blah" | php script.php > /tmp/output.txt
> STDIN: Named Pipe
> STDOUT: Regular File
> STDERR: Character Device
$ php script.php < /tmp/input.txt > /tmp/output.txt
> STDIN: Regular File
> STDOUT: Regular File
> STDERR: Character Device
$ php script.php 2> /tmp/error.log
> STDIN: Character Device
> STDOUT: Character Device
> STDERR: Regular File
$ php script.php 2>&1 >/tmp/output.txt
> STDIN: Character Device
> STDOUT: Regular File
> STDERR: Named Pipe