문제

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?

도움이 되었습니까?

해결책

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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top