如何让register_shutdown_function 设置的函数 在脚本中通过exit();结束的情况下不调用,只在意外中断时调用呢?

解决方案 »

  1.   


    <?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();
    ?>
    这是手册上的例子。
    if就可以了
      

  2.   

    Good,这是一个很好的思路,用标志位确认是正常退出,还是发生了异常。
      

  3.   

    <?php
    class Test {
       
       private $running;
       
       public function main() {
           $this->running = true; //函数一开始将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;//函数结束前将running设置为false,表示函数正常退出
       }   public function clean_exit() {
           if ($this->running) {
           // 仅当running标志为true(因为没有执行函数结束前的$this->running=true,也就表示程序没有正常退出)时执行。
               header("Location: error.php");    
           }    
       }
    }
    $t = new Test();
    $t->main();
    ?>
      

  4.   


    哦 明白了  可是  这个方法是在执行之前就预定了程序的结果,我register_shutdown_function设置的函数是一个完全独立的函数,想在程序执行过程中动态的影响他
      

  5.   

    谢谢,我已经解决了。
    不过还有一个问题,我在本地apache下超过20分钟后会触发register_shutdown_function设置的函数,可是在服务器的IIS下却不行,改怎么办呢?