【问题标题】:PHP command line output buffer outputs regardless of buffer settings无论缓冲区设置如何,PHP 命令行输出缓冲区输出
【发布时间】:2011-07-24 14:06:40
【问题描述】:

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

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

我也尝试过ob_start(false, 0, true);ob_end_clean() 等变体,但均无济于事。

我错过了什么?

【问题讨论】:

  • 我也遇到了同样的问题,我试过ob_implicit_flush(false); 并检查ob_start() 的结果以查看它是否启动,显然它会返回true。
  • 我也有同样的问题。尝试了隐式冲洗但没有骰子。

标签: php output-buffering


【解决方案1】:

你可能想要这样的东西

<?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();
}
?>

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 它在 php.ini 中设置为关闭,尽管它说它为 CLI SAPI 硬编码为 On。我添加了ob_implicit_flush(false); 无效。
    • 我不确定。如果您还没有调用ob_start(),可以尝试设置false
    【解决方案3】:

    以下内容为我解决了这个问题。在不调用 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-10-22
      • 2012-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多