我有一个非常具体的问题无法解决。
我有一个PHP系统,它由许多类组成。其中大多数是泛型类,通过使用自动加载处理程序加载,并在全局范围内手动创建(new运算符)。
但我也有一个主要且非常重要的singleton类,它也存在于全局范围中,并且是首先创建的。我想让它活到最后。这个类(首先创建)必须最后销毁。
这个类是一个错误处理类,它只有两个公共方法,用于管理错误、捕获异常、检查退出、发送报告等。
但是,这个类将首先销毁,因为它是最先创建的。
是否有可能影响该类的生存期,并使其在所有其他类都死亡后死亡?
谢谢。
更新扩展代码示例:
在分隔文件中定义的每个类
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();
如果您使用Zend Framework
,您可以执行以下操作:
Yours::initialize();
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini'
);
$application->bootstrap()
->run();
unset( $application);
Yours::destroy();
如果你使用自己的代码,你唯一的选择可能是:
Yours::initialize();
runApplication(); // This will contain all other objects in local scope and they
// will be destroyed before yours
Yours::destroy();
或者破解关闭处理程序,代码如下:
foreach( $GLOBALS as $key => $val){
unset( $GLOBALS[ $key]);
}
Yours::destroy();