无论缓冲区设置如何,PHP 命令行输出缓冲区输出

标签 php output-buffering

我有一些正在编写单元测试的类,其中有 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/

相关文章:

php - SOAP 方法调用极慢

php - 根据表中选中的动态复选框更新 MySQL 数据库中的数据

php - 在 AWS EC2 上重新启动 httpd 会出现异常。是不是要把/var/www/html的内容删掉?

php - 如何获取老客户名单

PHP:如何解决 ob_start() 结合 imagepng() 问题?

php - PHP 中用于增加页面加载时间的“输出缓冲”

php - 为什么 ob_get_contents 不起作用

php - 奇怪的 ob_start() 行为 - 双输出

php - 微时间到标准日期格式