2013-02-19 20 views
0

如何退出php腳本(例如使用exit()函數),但不觸發所有先前註冊的關閉函數(使用register_shutdown_function)?退出php命令而不觸發關機功能

謝謝!

編輯:或者,有沒有辦法清除所有註冊的關機功能?

+0

確保第一個註冊的關機功能包含調用exit我與一個非常複雜的系統工作的路徑() – 2013-02-19 11:10:42

+0

,我真的不知道這是第一個註冊的關斷功能。我只想退出而不調用關機功能,所以我可以很容易地進行調試。 – fstab 2013-02-19 11:13:36

+2

如果您想輕鬆調試,請使用調試器(如xdebug),您可以在其中設置斷點並檢查值等 – 2013-02-19 11:14:55

回答

4

如果進程使用SIGTERM或SIGKILL信號終止,則關閉函數將不會執行。

posix_kill(posix_getpid(), SIGTERM); 
2

請勿直接使用register_shutdown_function。創建一個管理所有關閉功能的類,該類具有自己的功能和啓用屬性。

class Shutdown { 

    private static $instance = false; 
    private $functions; 
    private $enabled = true; 

    private function Shutdown() { 
     register_shutdown_function(array($this, 'onShutdown')); 
     $this->functions = array(); 
    } 

    public static function instance() { 
     if (self::$instance == false) { 
      self::$instance = new self(); 
     } 

     return self::$instance; 
    } 

    public function onShutdown() { 
     if (!$this->enabled) { 
      return; 
     } 

     foreach ($this->functions as $fnc) { 
      $fnc(); 
     } 
    } 

    public function setEnabled($value) { 
     $this->enabled = (bool)$value; 
    } 

    public function getEnabled() { 
     return $this->enabled; 
    } 

    public function registerFunction(callable $fnc) { 
     $this->functions[] = $fnc; 
    } 

}