10

I have some classes I am writing unit tests for which have echoes in them. I want to suppress this output and thought ob_start() and ob_clean() would suffice, but they aren't having an effect.

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

I've also tried variations such as ob_start(false, 0, true); and ob_end_clean() to no avail.

What am I missing?

2
  • I've got the same issue, I've tried ob_implicit_flush(false); and checking the result of ob_start() to see if it starts, which apparently it does as it returns true. Commented Feb 1, 2012 at 12:43
  • 1
    I too am having the same issue. Tried the implicit flush but no dice. Commented Jun 24, 2014 at 11:37

3 Answers 3

3

you may want something like this

<?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();
}
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Do you have implicit_flush set to true in your PHP ini? This can cause the behaviour you are seeing as it tells PHP to tell the output layer to flush itself automatically after every output block. This is equivalent to calling the PHP function flush() after each and every call to print() or echo() and each and every HTML block.

2 Comments

It's set to off in php.ini, although it says it's hardcoded to On for the CLI SAPI. I've added ob_implicit_flush(false); to no effect.
I am not sure. Perhaps try setting it false before calling ob_start() if you haven't already.
0

The following solves this problem for me. Without calling ob_end_clean(), the contents of the buffer remain until the script's end, where it is flushed.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.