我有一些正在编写单元测试的类,其中有 echo 。我想抑制此输出并认为 ob_start()
和 ob_clean()
就足够了,但它们没有效果。
public function testSomething (){
ob_start();
$class = new MyClass();
$class->method();
ob_clean();
}
我也尝试过诸如 ob_start(false, 0, true);
和 ob_end_clean()
之类的变体,但均无济于事。
我错过了什么?
最佳答案
你可能想要这样的东西
<?php
public function testSomething (){
ob_start();
ob_implicit_flush(false); // turn off implicit flush
// Make your output below
$class = new MyClass();
$class->method();
// End of output
// store output into variable:
$output = ob_get_contents();
}
?>
关于无论缓冲区设置如何,PHP 命令行输出缓冲区输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5461175/