[英]PHP 7.2 Automatically enforcing callback prior to first byte transmission to Client
PHP 7.2:是否有一種方法可以在發送到客戶端的第一個字節之前強制“自動”調用php函數?
(HTML標記或其他任何內容),例如: songs.php
:
// Please ignore spelling mistakes, and work on concept alone.
require_once('sessionSetup.php');
require_once('setup_Pre_HTML_Tag_Transmission_Enforcer.php');
// The above has a function called: doMyHTMLTags();
doMyStuff(); // Setups, validations
doMoreStuff();
doHTMLContentDisplay();
// I need to execute doMyHTMLTags(), if and when any of the functions starts sending out displayable text.
示例:如果doMoreStuff
做一個DIE('No resources')
; 或者,如果doMyStuff
做了{ echo 'unexpected issue'; exit; },
{ echo 'unexpected issue'; exit; },
{ echo 'unexpected issue'; exit; },
我仍然需要執行doMyHTMLTags()
。
任何幫助,將不勝感激。
沒有嘗試過,但也許ob_start可以解決問題:
ob_start(
function($buffer) {
// nothing was produced
if (strlen($buffer) === 0) {
return false;
}
// prepend our string
return doMyHTMLTags() . $buffer;
}
);
doMyStuff(); // Setups, validations
doMoreStuff();
doHTMLContentDisplay();
如果doMyHTMLTags()
沒有返回字符串,但正在將其打印到瀏覽器,則可以嘗試執行此操作(但它將始終調用doMyHTMLTags
):
// get our string from output
ob_start();
doMyHTMLTags();
$my_html_tags = ob_get_clean();
ob_start(
function($buffer) use ($my_html_tags) {
// nothing was produced
if (strlen($buffer) === 0) {
return $buffer;
}
// prepend our string
return $my_html_tags . $buffer;
}
);
doMyStuff(); // Setups, validations
doMoreStuff();
doHTMLContentDisplay();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.