2012-02-08 26 views
1

我有一個非常具體的問題,我無法解決。PHP單程類生命週期破解

我有一個由多個類組成的PHP系統。它們大多數是泛型類,並使用自動加載處理程序加載,並在全局範圍內手動創建(新的運算符)。

但我也有一個主要的和非常重要的單例類,它也存在於全局範圍內並首先創建。我想讓它活到最後。這個類(首先創建)最後必須銷燬。

這個類是一個錯誤處理類,它只有兩個公共方法將被用於管理錯誤,捕獲異常,檢查口,發送報告等

但是,這個類將首先摧毀因爲它是第一個創建的。

是否有可能影響班級的生命並在所有其他班級死亡後使其死亡?

謝謝。

UPD 擴展碼樣本:

每個類中分離的文件中定義

class longlive { // error processing class 
    private function __construct() {} 

    public static function initialize() { 
     self::$instance = new longlive(); 
    } 

    public function __destruct() { 
     /// check for runtime session errors, send reports to administrators 
    } 

    public static function handler($errno, $errstr, $errfile = '', $errline = '', $errcontext = array()) { 
     /// set_error_handler set this method 
     /// process errors and store 
    } 

    public static function checkExit() { 
     /// register_shutdown_function will register this method 
     /// will trigger on normal exit or if exception throwed 
     /// show nice screen of death if got an uncoverable error 
    } 
} 

class some_wrapper { 
    public function __construct() {} 
    public function __destruct() {} 
} 

class core { 
    private function __construct() { 
     $this->wrapper = new some_wrapper(); 
    } 

    public static function initialize() { 
     self::$core = new core(); 
    } 
} 

腳本體:

include_once('path/to/longlive.php'); 
longlive::initialize(); 

include_once('path/to/core.php'); 
core::initialize(); 
+5

「安全原因「聽起來*非常不可能......我建議你看看依賴注入背後的概念:它們會讓你的代碼更容易,代碼更加靈活。 – lonesomeday 2012-02-08 18:56:53

+0

Singleton的安全?真?! – KingCrunch 2012-02-08 19:23:08

+0

你能否忘記關於未公開的安全原因並向我諮詢關於課程生命週期的問題?謝謝。 – ntvf 2012-02-08 19:24:51

回答

1

如果您使用如果您使用自己的代碼

Yours::initialize(); 
$application = new Zend_Application(
    APPLICATION_ENV, 
    APPLICATION_PATH . '/configs/application.ini' 
); 
$application->bootstrap() 
      ->run(); 
unset($application); 
Yours::destroy(); 

你唯一的選擇可能是:10,你可以做到這一點

Yours::initialize(); 
runApplication(); // This will contain all other objects in local scope and they 
        // will be destroyed before yours 
Yours::destroy(); 

或用這樣的代碼破解shutdown handler

foreach($GLOBALS as $key => $val){ 
    unset($GLOBALS[ $key]); 
} 

Yours::destroy(); 
+0

我不能在全局範圍內嚴格地創建類實例,它會在其內部進行初始化,請參閱示例代碼。 – ntvf 2012-02-08 19:19:00

+0

@ntvf固定爲靜態模型。) – Vyktor 2012-02-08 19:25:15

+0

謝謝你,對不起,我給不完整的描述。我可以不用關機處理程序來保持這個活動,查看更新的示例。 – ntvf 2012-02-13 10:44:20