Question

I am trying to get output buffering to persist through initializing a class and outputting the results of that class like shown below

class test { 
   function __construct(){
      ob_start();
   }  

   public function create(){
      echo '<div>';
      ob_flush();
      echo '</div>';
   }
}


$obj = new test();
echo 'hello';
$obj->create();

output

  <div>
  hello
  </div>

I want to have what is echoed between the object put into its output buffer. How would I go about doing something similar to this?

Was it helpful?

Solution

I think, you want something like this (DEMO)

class test { 
    function __construct(){
      ob_start();
    }  

    public function create(){
      $data=ob_get_clean();
      echo "<div style='color:red;'>".$data."</div>";
    }
}

$obj = new test();
echo 'hello';
$obj->create();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top