当前位置:首页 > 网络编程 > WEB编程 > PHP > php的register_shutdown_function详解

php的register_shutdown_function详解

点击次数:80 次 发布日期:2008-10-28 09:42:20 作者:源代码网
源代码网推荐

源代码网整理以下PHP提供register_shutdown_function()这个函数,能够在脚本终止前回调注册的函数

源代码网整理以下register_shutdown_function例子代码:

源代码网整理以下

以下为引用的内容:

源代码网整理以下 <?php
function Test()
{      if(!file_exists("Test.txt")){     //判断如果文件不存在!!
echo "文件不存在,我要创建一个:";
        $Str = fopen("Test.txt","w+");       
        fwrite($Str,"you are write after exit");
        fclose($Str);
echo "创建完成!";
}else {                           //如果存在;
        echo "文件已经存在";
}
}
register_shutdown_function("Test");
for($i=0;$i<10;$i++){
 echo "Echo<br/>";
}
exit;
?>

源代码网整理以下 There is a note "Shutdown function is called during the script shutdown so headers are always already sent.", but my php 5.1 seems to act differently.
Example:

源代码网整理以下

以下为引用的内容: <?php
class Test {
  
   private $running;
  
   public function main() {
       $this->running = true;
       ob_start();
       error_reporting(E_ALL);
       register_shutdown_function(array($this, "clean_exit"));
       echo "Hello";
       // triggers E_ERROR
       $fatal->error();
      
       $this->running = false;   
   }

源代码网整理以下 

源代码网整理以下   public function clean_exit() {
       if ($this->running) {
           header("Location: error.php");   
       }   
   }
}
$t = new Test();
$t->main();
?>
This example redirects you on error.php, this could be a simple way to handle E_ERROR.

源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华