2012-02-08 72 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