-4

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?

Rujikin
  • 730
  • 2
  • 10
  • 17

1 Answers1

2

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();
The Alpha
  • 143,660
  • 29
  • 287
  • 307