因此ob_start()
應該捕獲輸出,直到另一個緩衝區函數被調用,如ob_get_clean()
,ob_get_contents()
,ob_get_flush()
。當發生錯誤時ob_start被中斷
但是,當緩衝區讀取器內部拋出異常時,它會通過停止它並回顯輸出而不是繼續捕獲它來影響讀取器。這是我想要防止的。
比方說這是我的腳本:
<?php
error_reporting(0);
try {
ob_start();
echo "I don't wanna output this what so ever, so want to cache it in a variable with using ob_ functions";
$unlink = unlink('some file that does not exist');
if(!$unlink) throw new Exception('Something really bad happen.', E_ERROR); //throwing this exception will effect the buffer
$output = ob_get_clean();
} catch(Exception $e) {
echo "<br />Some error occured: " . $e->getMessage();
//print_r($e);
}
?>
這個腳本會輸出:
I don't wanna output this what so ever, so want to cache it in a variable with using ob_ functions
Some error occurred: Something really bad happen.
當它的假設只是打印
Some error occurred: Something really bad happen.
我在做什麼錯,是有解決方案嗎?
嘗試移動'嘗試... catch'塊'ob_start()'之外。 – vee
@vinodadhikary感謝您的評論,這可以工作,但會導致錯誤消息得到緩衝。我不希望這種情況發生。 – Junior