Redirecting console output

at 2009-07-12 in Examples by friebe (0 comments)

To capture output written by any of the PHP functions the ouput control functions can be used (ob_start(), ob_get_contents() and friends). These functions do not affect the output made by the util.cmd.Console class as this writes directly to the standard out and error streams. To redirect these, you can use the following:

  // Save original stream in variable, change output stream to $stream
$out= Console::$out->getStream();
Console::
$out->setStream($stream);

// Write "Hello" and a newline, this will trigger $stream's write() method
Console::writeLine('Hello');

// Finally, restore original output stream
Console::$out->setStream($out);

This can be useful in unittests, when running command line applications inside a web container or inside GUIs, or whatever other usecase you can invent:-)



Subscribe

You can subscribe to the XP framework's news by using RSS syndication.


Categories

News
General
PHP5
Announcements
RFCs
Further reading
Examples
Editorial
EASC
Experiments
Unittests
Databases
5.8-SERIES
Unicode
Language
5.9-SERIES

Related

Find related articles by a search for «Redirecting».