9

我有一些课程正在编写单元测试,其中有回声。我想抑制这种输出和想法ob_start()ob_clean()就足够了,但它们没有效果。

public function testSomething (){
    ob_start();
    $class = new MyClass();
    $class->method();
    ob_clean();
}

我也尝试过诸如但无济于事的变ob_start(false, 0, true);ob_end_clean()

我错过了什么?

4

3 回答 3

2

你可能想要这样的东西

<?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();
}
?>
于 2011-03-28T15:27:57.853 回答
0

你的 PHP ini 中是否设置了implicit_flush ?true这可能会导致您看到的行为,因为它告诉 PHP 告诉输出层在每个输出块之后自动刷新自身。这相当于在每次调用 print() 或 echo() 以及每个 HTML 块之后调用 PHP 函数 flush()。

于 2011-03-28T15:29:25.920 回答
0

以下为我解决了这个问题。在不调用 ob_end_clean() 的情况下,缓冲区的内容会一直保留到脚本结束时,它会被刷新。

ob_implicit_flush(false);
ob_start();    
/*
  ...
  ... do something that pushes countent to the output buffer
  ...
*/    
$rendered = ob_get_contents();
ob_end_clean(); // Needed to clear the buffer
于 2018-07-22T20:16:02.090 回答