2009-09-29 30 views
2

我有一組定製的PHP腳本。他們加載一個主要的PHP文件(包含其他文件,設置設置等)。從這個腳本中,我想插入一個回調函數,該函數在結束之前由包含主文件的腳本調用。腳本死前的PHP回調

我打算做的是製作一個簡單的定製輸出緩存ob_get_contents()。我想在main.php中執行ob_start(),然後讓回調函數存儲輸出。

+0

請把這個改爲一個問題。 – Asaph 2009-09-29 03:35:57

回答

1

ob_start()可以做你想做什麼:

ob_start('store_output'); 

function store_output($output) { 
    // do something with $output 
    return $output; 
} 

當輸出緩衝區被刷新時,該函數將被調用。輸出緩衝區可以嵌套。

+0

如果你叫ie,會發生什麼。 ob_get_clean()? – 2009-09-29 03:42:59

+0

它會調用回調。這是所需的行爲? – cletus 2009-09-29 03:45:38

+0

糟糕,我應該先看看PHP文檔:)。這正是我需要的! – heyitsme 2009-09-29 03:57:03

4

register_shutdown_function

上 關機註冊的執行的功能從手動

例子:

<?php 
function shutdown() 
{ 
    // This is our shutdown function, in 
    // here we can do any last operations 
    // before the script is complete. 

    echo 'Script executed with success', PHP_EOL; 
} 

register_shutdown_function('shutdown'); 
?>