我有一个非常具体的问题,我无法解决。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();
“安全原因“听起来*非常不可能......我建议你看看依赖注入背后的概念:它们会让你的代码更容易,代码更加灵活。 – lonesomeday 2012-02-08 18:56:53
Singleton的安全?真?! – KingCrunch 2012-02-08 19:23:08
你能否忘记关于未公开的安全原因并向我咨询关于课程生命周期的问题?谢谢。 – ntvf 2012-02-08 19:24:51