OB_START () PHP ၏ output buffering ယန္တရားကိုအသုံးပြုသောအခါအများအားဖြင့်အသုံးပြုသောလုပ်ဆောင်ချက်တစ်ခုဖြစ်သည်။ developer များသည် template processing processing, cache သို့မဟုတ် compression အတွက် output ကိုအကြောင်းအရာများကိုကြားဖြတ်ရန်ခွင့်ပြုသည်။ သို့သော်ရှုပ်ထွေးသောစီမံကိန်းများတွင်အထူးသဖြင့် Middleware, Template or or or romptications များသည် Ob_start မှမကြာခဏခေါ်ဆိုမှုများဖြစ်ပေါ်လာသောအခါတိုင်းမူကြမ်းမှီခိုမှုပြ Prots နာများကိုဖြစ်ပေါ်စေပြီး,
ဤအရာကိုရှောင်ရှားရန် PHP သည် OB_List_Handlers () လက်တွေ့လုပ်ဆောင်မှုကိုထောက်ပံ့ပေးသည်။ ၎င်းသည်လက်ရှိ activated output buffer processor များအားလုံးကိုစာရင်းပြုစုရန်ခွင့်ပြုသည်။
Ob_start နှင့်ပ conflict ိပက်ခများသို့မဟုတ်မြို့ပတ်ရထားမှီခိုမှုကိုရှောင်ရှားရန် Ob_list_Handlers () ကို မည်သို့အသုံးပြုရမည်ကိုရှင်းပြရန်ဥပမာတစ်ခုကိုဤဆောင်းပါးသည်ဥပမာတစ်ခုကိုအသုံးပြုပါမည်။
ကျွန်ုပ်တို့သည် template system ထဲတွင်ပြန်ဆိုခြင်းမပြုမီ ob_start () ကို ခေါ်ဆိုသော်လည်းအချို့သောအစိတ်အပိုင်းများသို့မဟုတ် plug-ins များကိုလည်း ob_start () ကို အသုံးပြု. မပိတ်နိုင်ပါ။
function renderTemplate($templateFile) {
ob_start();
include $templateFile;
return ob_get_clean();
}
အကယ်. အစိတ်အပိုင်းကို template file တစ်ခုတွင်ထပ်မံခေါ်ယူပါကအစိတ်အပိုင်းသည် OB_Start ကိုအသုံးပြုသည် () သည် OB_START ကို အသုံးပြုသည်။
ထပ်ခါတလဲလဲသို့မဟုတ်မလိုအပ်သော output ကိုကြားခံခြင်းကိုကာကွယ်ရန်အတွက် ob_start ကို မခေါ်မီလက်ရှိကြားခံ stack တွင်အသုံးပြုသောပရိုဆက်ဆာပါ 0 င်သည်ကိုသင်စစ်ဆေးနိုင်သည်။
function safe_ob_start($handler = null) {
$currentHandlers = ob_list_handlers();
// မလုပ်မနေရမဟုတ်သော:တူညီသောပရိုဆက်ဆာ၏ထပ်ခါတလဲလဲ activation ကိုရှောင်ကြဉ်ပါ
if ($handler && in_array($handler, $currentHandlers)) {
// enabled,skip Start
return false;
}
// Processor ကိုအမည်မပေးထားပါက,အလွှာတစ်ခုထက်ပိုပြီးအသိုက်မသေချာအောင်လုပ်ပါ
if (count($currentHandlers) >= 5) {
error_log("ob_start အလွှာလွန်း,အသိုက်ပြ issues နာများရှိနိုင်ပါသည်: https://gitbox.net/docs/ob_start-limit");
return false;
}
ob_start($handler);
return true;
}
ဒီ function မှာငါတို့:
လက်ရှိကြားခံပရိုဆက်ဆာများအားလုံးကိုရယူရန် OB_List_Handlers () ကို သုံးပါ။
ပုံတူပွားခြင်းကိုရှောင်ရှားရန်တူညီသောပရိုဆက်ဆာကိုဖွင့်ထားသလားစစ်ဆေးပါ။
Buffer အလွှာအများဆုံးအရေအတွက်ကိုထိန်းချုပ်ရန် (ဤနေရာတွင်ဤနေရာတွင်ဤနေရာတွင်ဥပမာအဖြစ် 5 ကိုကြည့်ပါ),
အကယ်. သင်သည်အဘယ်သူမျှမချွင်းချက်မရှိဆုံးဖြတ်လျှင် ob_start () ကို လုံခြုံစွာခေါ်ဆိုပါ။
function renderView($viewPath) {
// Safe Start Buffering
if (!safe_ob_start()) {
throw new Exception("output ကိုကြားခံဖွင့်လို့မရပါ,ပ conflicts ိပက်ခများရှိနိုင်သည်!");
}
include $viewPath;
return ob_get_clean();
}
// အသုံးရောင်
try {
$content = renderView(__DIR__ . '/views/home.php');
echo $content;
} catch (Exception $e) {
error_log($e->getMessage());
header("Location: https://gitbox.net/error");
}
ob_list_handlers () ကို အသုံးပြုခြင်း () သည် output buffering တိုက်မှုကိုကာကွယ်ရန်ထိရောက်သောနည်းလမ်းဖြစ်သည်။ ၎င်းသည် Bunfered ပြည်နယ်တွင်ပွင့်လင်းမြင်သာသောသတင်းအချက်အလက်များကိုကူညီနိုင်ပြီးကျွန်ုပ်တို့ကိုကူညီခြင်း,
Ob_start မှထပ်ခါတလဲလဲခေါ်ဆိုမှုများကိုရှောင်ကြဉ်ပါ။
Control Buffer အသိုက်အဆင့်ဆင့်;
Debug output output buffering ဆက်စပ်သောပြ issues နာများကိုမြှင့်တင်ရန်။
Safe_ob_start () ကဲ့သို့သောလုပ်ဆောင်ချက်များကို encapsulating လုပ်ခြင်းဖြင့် PHP Projects များသည် festing ပရမ်းပတာဖြစ်ခြင်းကြောင့်ဖြစ်ပေါ်လာသောခက်ခဲသောအမှားများကိုရှောင်ရှားရန် output buffering ကိုပိုမိုလွယ်ကူစွာအသုံးပြုနိုင်သည်။
output buffer ပြည်နယ်ကို debug လုပ်သည့် Visual Tool စာမျက်နှာကိုထုတ်လုပ်ရန်ကျွန်ုပ်လိုအပ်ပါသလား။